[英]Regular expression for 5 comma separated email id
I trying to validate 5 comma separated email id in one regular expression. 我试图在一个正则表达式中验证5个逗号分隔的电子邮件ID。 I currntly using below regex 我在下面使用正则表达式
^([\w+-.%]+@[\w-.]+\.[A-Za-z]{2,4},?)+$
This is valid for one email id. 这对于一个电子邮件ID有效。
I would like to know how I can achieve the same, any small inputs on the same is also greatly appreciated. 我想知道如何实现相同的目标,对同一目标的任何小投入也将不胜感激。
Thanks in advance. 提前致谢。
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). 因此,您的正则表达式的电子邮件部分应为[-\\w+.%]+@[\\w-.]+\\.[A-Za-z]{2,4}
(请注意-
在第一个字符中的位置类,在第二种情况下,可以将其放在速记字符类\\w
和下一个字符之间)。
Next, to match 1 to 5 comma-separated emails, you need to match the first one, and then match 0 to 4 emails. 接下来,要匹配1到5个逗号分隔的电子邮件,您需要匹配第一个,然后匹配0到4个电子邮件。 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}$
Basically, ^<EMAIL>(?:,<EMAIL>){0,4}$
: 基本上, ^<EMAIL>(?:,<EMAIL>){0,4}$
:
^
- start of string ^
-字符串的开头 <EMAIL>
- an email pattern of yours <EMAIL>
-您的电子邮件格式 (?:
- start of a non-capturing group acting as a container for a sequence of patterns: (?:
-一个非捕获组的开始,它充当一系列模式的容器:
,
- a comma ,
-逗号 <EMAIL>
- an email pattern of yours <EMAIL>
-您的电子邮件格式 ){0,4}
- zero to four occurrences of these sequences above ){0,4}
-上述序列中零到四次出现 $
- end of string. $
-字符串结尾。 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!"); }
在Java脚本的正则表达式下,您可以使用多个逗号分隔的电子邮件ID,希望此功能对您有用
/^(\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]{2,4}\s*?,?\s*?)+$/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.