5个逗号分隔的电子邮件ID的正则表达式 [英] Regular expression for 5 comma separated email id
问题描述
我试图用一个正则表达式验证5个逗号分隔的电子邮件ID. 我在下面使用正则表达式
I trying to validate 5 comma separated email id in one regular expression. I currntly using below regex
^([\w+-.%]+@[\w-.]+\.[A-Za-z]{2,4},?)+$
这对于一个电子邮件ID有效.
This is valid for one email id.
我想知道如何实现相同的目标,对同一目标的任何小投入也将不胜感激.
I would like to know how I can achieve the same, any small inputs on the same is also greatly appreciated.
谢谢.
推荐答案
首先,修复模式:-
在字符类的两个字符之间形成一个范围.因此,您的正则表达式的电子邮件部分应为[-\w+.%]+@[\w-.]+\.[A-Za-z]{2,4}
(请注意-
在第一个字符类中的位置,在第二个字符类中,可以将其放在速记字符类\w
和下一个字符之间)
First of all, fix the pattern: -
in between two chars inside a character class forms a range. So, the email part of your regex should be [-\w+.%]+@[\w-.]+\.[A-Za-z]{2,4}
(note the position of -
in the first character class, in the second, it is OK to put it between a shorthand character class \w
and the next char).
接下来,要匹配1到5个逗号分隔的电子邮件,您需要匹配第一个,然后匹配0到4个电子邮件.并在模式周围添加锚点,以确保模式与整个字符串匹配:
Next, to match 1 to 5 comma-separated emails, you need to match the first one, and then match 0 to 4 emails. And add anchors around the pattern to make sure the pattern matches the whole string:
^[-\w+.%]+@[\w-.]+\.[A-Za-z]{2,4}(?:,[-\w+.%]+@[\w-.]+\.[A-Za-z]{2,4}){0,4}$
基本上是^<EMAIL>(?:,<EMAIL>){0,4}$
:
-
^
-字符串开头 -
<EMAIL>
-您的电子邮件格式 -
(?:
-一个非捕获组的开始,该组充当一系列模式的容器:-
,
-逗号 -
<EMAIL>
-您的电子邮件格式
^
- start of string<EMAIL>
- an email pattern of yours(?:
- start of a non-capturing group acting as a container for a sequence of patterns:,
- a comma<EMAIL>
- an email pattern of yours
另一个想法是使用
,
进行拆分,然后进行验证:Another idea is to split with
,
and then validate:var s = "abc@gg.com,abc2@gg.com,abc3@gg.com,abc4@gg.com,abc5@gg.com"; var re = /^[-\w+.%]+@[\w-.]+\.[A-Za-z]{2,4}$/; var items = s.split(","); if (items.length <= 5 && items.filter(function(x) { return re.test(x); }).length === items.length ) { console.log("VALID => ", items); } else { console.log("INVALID!"); }
这篇关于5个逗号分隔的电子邮件ID的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-