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})*)$
精彩评论