简体   繁体   English

逗号分隔的正则表达式

[英]comma separated regex

I have an email regex I am happy with, lets call it EMAIL. 我有一个满意的电子邮件正则表达式,可以称之为EMAIL。 So my field validator looks like this 所以我的现场验证器看起来像这样

var regex=/^EMAIL$/

But now the Captain tells me he wants to allow comma separated emails in the field. 但是现在船长告诉我他想允许在该字段中使用逗号分隔的电子邮件。

What first comes to mind is something like 首先想到的是

/^(EMAIL)?(, EMAIL)*$/

but that repeats EMAIL, which already offends my senses. 但是重复EMAIL,这已经冒犯了我的感官。 Is there a way to write this regex without repeating EMAIL? 有没有一种方法可以编写此正则表达式而无需重复EMAIL?

用逗号分隔值,然后在每一个上使用正则表达式。

This covers all the examples mentioned 这涵盖了所有提到的例子

^(EMAIL(,\s|$))+$

It matches 它匹配

EMAIL
EMAIL, EMAIL

but not 但不是

EMAILEMAIL

I can't think of a way to match email list as it is , without repeating EMAIL. 我不能想办法来匹配的电子邮件列表, 因为它是不重复EMAIL。 Here is a simple workaround: 这是一个简单的解决方法:

/^(EMAIL, )+$/.test(emailList + ", ");

Example: 例:

var s = "abc, def, ghi, jkl";
/^([a-z]+, )+$/.test(s + ", ");

杰森(Jason's)似乎是最简单的,但是就其价值而言,您还可以使用反向引用^EMAIL, EMAIL$将与^(EMAIL), \\1$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM