开发者

comma separated regex

I have an email regex I am happy with, lets call it EMAIL. So my field validator looks like this

var regex=/^EMAIL$/

But now the Captain tells me he wants t开发者_如何转开发o 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. Is there a way to write this regex without repeating EMAIL?


This covers all the examples mentioned

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

It matches

EMAIL
EMAIL, EMAIL

but not

EMAILEMAIL


Split the value by comma, then use the regex on each one.


I can't think of a way to match email list as it is, without repeating EMAIL. Here is a simple workaround:

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

Example:

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


Jason's seems the simplest, but for what it's worth, you could also use backreferences: ^EMAIL, EMAIL$ would be the same as ^(EMAIL), \1$.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜