I’m looking for a way to extract the email address from a string I have already stored in a Google Tag Manager variable. I’m new with Javascript and I tried some functions I found on the internet but they all return “undefined”
Example :
function findEmailAddresses(StrObj) { var separateEmailsBy = ", "; var email = "<none>"; // if no match, use this var emailsArray = StrObj.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi); if (emailsArray) { email = ""; for (var i = 0; i < emailsArray.length; i++) { if (i != 0) email += separateEmailsBy; email += emailsArray[i]; } } return email; }
My string is : ‘ You are now logged as John Doe (john.doe@gmail.com) ’ (the incorrect caracters are linked with fontawesome library problem, fixed soon) I would like to run a JS/Tag Manager function that return only john.doe@gmail.com
The Google Tag Manager functions should not use librairies and should be a JavaScript function that returns a value using the ‘return’ statement. Thanks for you help.
Regards.
Advertisement
Answer
This should do the job for your String
:
strObj.match( '(?<=\()[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+(?=\))' )[0];