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 :
JavaScript
x
14
14
1
function findEmailAddresses(StrObj) {
2
var separateEmailsBy = ", ";
3
var email = "<none>"; // if no match, use this
4
var emailsArray = StrObj.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
5
if (emailsArray) {
6
email = "";
7
for (var i = 0; i < emailsArray.length; i++) {
8
if (i != 0) email += separateEmailsBy;
9
email += emailsArray[i];
10
}
11
}
12
return email;
13
}
14
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
:
JavaScript
1
2
1
strObj.match( '(?<=\()[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+(?=\))' )[0];
2