简体   繁体   English

从 Javascript 中的字符串中提取 email 地址(谷歌标签管理器功能)

[英]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.我正在寻找一种方法来从我已经存储在 Google 跟踪代码管理器变量中的字符串中提取 email 地址。 I'm new with Javascript and I tried some functions I found on the internet but they all return "undefined"我是 Javascript 的新手,我尝试了一些在互联网上找到的功能,但它们都返回“未定义”

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我的字符串是:'您现在被记录为 John Doe (john.doe@gmail.com)'(不正确的字符与 fontawesome 库问题相关联,很快就会修复)我想运行一个只返回的 JS/标签管理器 function 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. Google 跟踪代码管理器函数不应使用库,并且应该是使用“return”语句返回值的 JavaScript function。 Thanks for you help.谢谢你的帮助。

Regards.问候。

This should do the job for your String :这应该为您的String完成工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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