简体   繁体   中英

Extract email address from string in Javascript (Google Tag manager function)

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.

This should do the job for your String :

strObj.match( '(?<=\\()[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\\.[a-zA-Z0-9._-]+(?=\\))' )[0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM