【问题标题】:How can I create a JavaScript regex for finding email addresses in strings?如何创建用于在字符串中查找电子邮件地址的 JavaScript 正则表达式?
【发布时间】:2021-06-23 01:06:19
【问题描述】:

我有一个由收件箱中的电子邮件触发的逻辑应用。一切正常,除了一些电子邮件在我不想要的时候通过。或者更确切地说,带有 image001.png@01D766B1.7C184990 图像描述的电子邮件签名正在通过。 我认为可能是我的正则表达式允许它,但我对正则表达式不是很好。

这是我目前的代码:

var reg = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi;
var emailData = " \n\n'Phonenumber2@test.com'\n\n \n\n  DevOps\n[cid:image001.png@01D766B1.7C184990]\n\n ";
    
//Matching email signatures
var matches = emailData .match(reg);

console.log(matches);

我需要正则表达式来返回任何电子邮件地址的列表,但它们需要完全形成。与上面提到的缺少 .com(或 .org 等)不同。

【问题讨论】:

  • 你的正则表达式返回["Phonenumber2@test.com", "image001.png@01D766B1.7C184990"],那么问题出在哪里?
  • 你想说image001.png@01D766B1.7C184990不是一个有效的电子邮件?最后一个. 后面可能没有数字或限制最后一个. 之后的字符数?
  • 我不想要第二个字符串,因为它不是电子邮件地址。我只想要看起来像有效的电子邮件地址。

标签: javascript regex


【解决方案1】:

您的正则表达式(允许具有 @. 的所有内容)

const regex = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gm;
const str = `Phonenumber2@test.com
Phonenumber2@test.info
image001.png@01D766B1.7C184990
Phonenumber2@test.org`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    console.log(m[0]);
}

#1 最后一个.之后不允许有数字

const regex = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z_-]+)/gm;
const str = `Phonenumber2@test.com
Phonenumber2@test.info
image001.png@01D766B1.7C184990
Phonenumber2@test.org`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    console.log(m[0]);
}

#2 将最后一个 . 之后的字符限制为最少 2 个和最多 7 个字符 {2,7}$

const regex = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]{2,7}$)/gm;
const str = `Phonenumber2@test.com
Phonenumber2@test.info
image001.png@01D766B1.7C184990
Phonenumber2@test.org`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    console.log(m[0]);
}

#3 定义可能的顶级域名列表,例如(com|org|info)

const regex = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.(com|org|info)$)/gm;
const str = `Phonenumber2@test.com
Phonenumber2@test.info
image001.png@01D766B1.7C184990
Phonenumber2@test.org`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    console.log(m[0])
}

【讨论】:

  • 感谢您的解决方案!我有一个问题,我认为它的输入。我从逻辑应用程序步骤中传递了一个字符串,该步骤从电子邮件中删除了 HTML,它看起来像这样: var email = "EmailBot.Queue@company.com>\nSubject: FW:\n\n \n\ n \n\n'Phonenumber2@test.com'\n\n \n\n 电子邮件签名 | 开发团队\n[cid:image001.png@01D766B1.7C184990]\n\n";当我通过我的脚本运行它时,它没有匹配。
  • 我明白了。我只是用替换来取出我想忽略的任何字符。
  • @evolmonster 问题很可能是最后的$$ 确保结束。使用这个正则表达式工具来练习并获得解释:regex101.com/r/HCM4y7/1
【解决方案2】:

看看 - https://ihateregex.io/expr/email/

不想打扰你,但通过正则表达式进行电子邮件匹配即使不是不可能也很难。

一种方法是匹配带有域名TDN 结尾的东西(您可以创建一组所有 tdn 并匹配或仅限制正则表达式的结尾部分 - modified regex 并从那里开始过滤掉它。

【讨论】:

  • 这个特殊的正则表达式是天蓝色逻辑应用程序的一部分。我从发送到某个收件箱的电子邮件正文中阅读了电子邮件。我认为正则表达式/javascript 将是要走的路。但是,如果您说正则表达式无法捕获所有内容,也许我需要另一种解决方案?
猜你喜欢
  • 2012-02-23
  • 2013-04-09
  • 1970-01-01
  • 2011-08-03
  • 2022-10-22
  • 2013-07-06
  • 2011-08-24
  • 2011-12-28
  • 2013-08-09
相关资源
最近更新 更多