开发者

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

开发者 https://www.devze.com 2023-01-24 00:25 出处:网络
I have written a JavaScript regular expression to validate a comma separated list of tags.开发者_运维问答

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

精彩评论

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