用于GMail样式电子邮件地址验证的Javascript正则表达式



我需要一个正则表达式,该表达式将接受多种格式的格式良好的电子邮件(见下文),这些电子邮件将以逗号分隔的列表中输入。我有基本的电子邮件地址验证正则表达式,

^[wd._%+-]+@(?:[wd-]+.)+(w{2,})(,|$) 

它可以处理测试用例 AB,但不能处理其他用例。我也试过

^(<)?[wd._%+-]+@(?:[wd-]+.)+(w{2,})(>)?(,|$)

它能够处理 ABC,但只验证了每个测试用例 DE 中的第一个电子邮件地址。我什至还没有测试格式 3 的正则表达式。

dr 需要一个正则表达式来验证电子邮件地址 123

测试正则

表达式的好网站:在线Javascript正则表达式测试器

数据

测试用例
A. nora@example.com
B. nora@example.com, fred@example.com
C. <nora@example.com> , fred@example.com
D.
<nora@example.com>, <fred@example.com> E. fred@example.com, <nora@example.com>

电子邮件地址格式
1. xyz@example.com
2. <xyz@example.com>
3. "XYZ"<xyz@example.com>

编辑

我将其标记为可能的重复项:

在 JavaScript 中验证电子邮件地址?

反过来,这似乎是以下内容的副本:

使用正则表达式验证电子邮件地址

两者都包含了很多关于正则表达式作为电子邮件验证的有效性的讨论。但是,提供的投票最多的正则表达式似乎并没有完全按照我的意愿进行,因此我认为这还没有答案。

提供的链接或答案都不是这个问题的最佳答案。这是解决它的方法:

/*
* regex checks: must start with the beginning of a word or a left caret
* must end with either the end of a word or a right caret
* can handle example.example.com as possible domain
* email username can have + - _ and .
* not case sensitive
*/
var EMAIL_REGEX = /(<|^)[wd._%+-]+@(?:[wd-]+.)+(w{2,})(>|$)/i;  
var emails = emailList.trim().split(',');  
var validEmails = [];  
var invalidEmails = [];  
for (var i = 0; i < emails.length; i++) {  
    var current = emails[i].trim();
    if(current !== "") {
        //if something matching the regex can be found in the string
        if(current.search(EMAIL_REGEX) !== -1) {
            //check if it has either a front or back bracket
            if(current.indexOf("<") > -1 || current.indexOf(">") > -1) {
                //if it has both, find the email address in the string
                if(current.indexOf("<") > -1 && current.indexOf(">") > -1) {
                    current = current.substr(current.indexOf("<")+1, current.indexOf(">")-current.indexOf("<") -1);
                } 
            } 
        }
        if(EMAIL_REGEX.test(current)) {
            validEmails.push(current);
        } else {
            invalidEmails.push(current);
        }
    }               
}

首先将逗号分隔的列表拆分为一个数组,然后单独验证数组的每个成员会更简单。这将使正则表达式更易于编写(以及读取和维护),并且还使您能够向输入列表的用户提供特定反馈("第三个电子邮件地址无效")。

所以假设你通过拆分这样做

var bits = csv.split(',');

循环访问bits数组

for (var i = 0; i < bits.length; ++i) {
  if (!validateEmail(bits[i])) {
    alert("Email #" + (i+1) + " is bogus");
  }
}

然后对于正则表达式,这样的东西将捕获 2 和 3

("[a-z0-9s]+"s+)?<[wd._%+-]+@(?:[wd-]+.)+(w{2,})>

您可以使用更简单的电子邮件地址来捕获简单的电子邮件地址,而无需<或前面引号中的名称。

单个正则表达式的运行速度不一定比两个if测试快,特别是如果您通过将更有可能的正则表达式放在第一位来短路or。它也更难阅读和维护。最后,这非常棘手,因为您需要提前查看:只有当电子邮件地址前面的字符串包含电子邮件第一个字符之前的<时,最终>才可以。

所以我的 0.02 美元 = 不值得。只需执行两个正则表达式。

此 validateEmail 函数将检查电子邮件地址的基本语法 ( xyz@example.com )。包含的if将检查备用格式(<xyz@example.com>, 'xyz' <xyz@example.com>),并且仅验证实际的电子邮件部分。只有<或>的项目因格式不佳而被视为无效(Nope@example.com>),与任何缺乏所需基本结构的电子邮件(invalidExample.com)相同。

var emailList = "abc@example.com,<lmn@example.com>,'xyz' <xyz@example.com>,invalidExample.com,Nope@example.com>,'Still93e-=48%5922=2 Good' <xyz@example.com>";
var emails = emailList.split(",");
//Loop through the array of emails
for (var i = 0; i < emails.length; i++) {
    var isValid = 1;
    var cur = emails[i];
    // If it has a < or a >,
    if( cur.indexOf("<") > -1 || cur.indexOf(">") > -1 ){
        // Set it invalid
        isValid = 0;
        // But if it has both < and >
        if( cur.indexOf("<") > -1 && cur.indexOf(">") > -1 ){
            //Set it valid and set the cur email to the content between < and >
            isValid = 1;
            cur = cur.substr(cur.indexOf("<")+1, ( cur.indexOf(">") - cur.indexOf("<") - 1 ));
        }
    }
    //Run the validate function
    if ( !validateEmail(cur) )
        isValid = 0;
    // Output your results. valid = 1, not valid = 0
    alert("Orig: "+emails[i] +"nStripped: "+cur+"nIs Valid: "+isValid);
}
function validateEmail(curEmail){
    var emailValid = /.*@.*..*$/g;
    return (curEmail.test(emailValid));
}

js小提琴

这样

的事情会有帮助吗?

我已经测试了 2. 和 3.,它检测到两种模式。

var isEmail_re       = /^s*[w-+_]+(.[w-+_]+)*@[w-+_]+.[w-+_]+(.[w-+_]+)*s*$/;
function isEmail (s) {
   return String(s).search (isEmail_re) != -1;
}
alert(isEmail ('"xyz"<xyz@example.com>'));

http://jsfiddle.net/epinapala/BfKrR/

最新更新