开发者

Entending my JavaScript regular expression for a comma separated list of tags to allow whitespace

I have written a JavaScript regular expression to validate a comma separated list of tags.开发者_运维问答 A tag can contain characters A-Z, a-z, 0-9, space, hyphen and apostrophe, and is 3-50 characters long.

Here is what I have:

^[a-zA-Z0-9\- ']{3,50}(,[a-zA-Z0-9\- ']{3,50})*$

I want to extend this to allow the user to provide no tags at all. However, if I put a question mark after the first tag section of the regex, it will allow things like ,tag

How would I make it allow whitespace or the above? (I'd say allow it to be blank or the above, but as I'm going to trim the entry, I'll be lenient and say whitespace is OK)


Here is a try:

^([-a-zA-Z0-9 ']{3,50},)*([-a-zA-Z0-9 ']{3,50})?$


^(\s*|[a-zA-Z0-9\- ']{3,50}(,[a-zA-Z0-9\- ']{3,50})*)$
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜