用于GMail样式的电子邮件地址验证的Javascript正则表达式 [英] Javascript regex for GMail-style email address validation
问题描述
我需要一个正则表达式,该表达式可以接受多种格式的电子邮件(请参见下文),并以逗号分隔的列表形式输入。我有基本的电子邮件地址
验证正则表达式,
I need a regular expression that will accept well-formed emails in several formats (see below) that will be input in a comma-separated list. I have the basic email address validation regex,
^[\w\d._%+-]+@(?:[\w\d-]+\.)+(\w{2,})(,|$)
可以处理测试用例 A 和 B ,但不能处理其他用例。我也尝试过
which can handle test cases A and B, but not the others. I also tried
^(\<)?[\w\d._%+-]+@(?:[\w\d-]+\.)+(\w{2,})(\>)?(,|$)
能够处理 A , B 和 C ,但仅在每个测试用例 D 和 E 中验证了第一个电子邮件地址。我什至还没有测试格式为 3 的正则表达式。
which was able to handle A, B, and C, but only validated the first email address in each of test cases D and E. I haven't even gotten to testing a regex for format 3.
tl; dr 需要一个正则表达式来验证电子邮件地址 1 , 2 ,以及 3 。
tl;dr Need a regex that will validate email addresses 1, 2, and 3.
测试您的正则表达式的好网站:在线Javascript正则表达式测试器
Good website to test your regular expressions: Online Javascript Regex Tester
数据
Data
测试用例
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> ;
Test Cases
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>
我已标记这可能是以下内容的重复项:
I flagged this as a possible duplicate of:
似乎与以下内容重复:
两者都包含很多有关正则表达式作为电子邮件验证的有效性的讨论。但是,提供的投票最多的正则表达式似乎并没有达到我想要的功能,因此我还没有考虑到这个答案。
both of which contain much discussion on the validity of regex as email validation. However, the top-voted regexes provided don't seem to do quite what I want so I don't consider this answered yet.
推荐答案
提供的链接或答案均不是此问题的最佳答案。解决问题的方法如下:
None of the links or answers provided was the best answer for this question. Here's what solved it:
/*
* 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 = /(\<|^)[\w\d._%+-]+@(?:[\w\d-]+\.)+(\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);
}
}
}
这篇关于用于GMail样式的电子邮件地址验证的Javascript正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!