简体   繁体   English

输入我的JavaScript正则表达式以逗号分隔的标签列表以允许空白

[英]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. 我已经编写了JavaScript正则表达式来验证以逗号分隔的标签列表。 A tag can contain characters AZ, az, 0-9, space, hyphen and apostrophe, and is 3-50 characters long. 标签可以包含字符AZ,az,0-9,空格,连字符和撇号,并且长度为3-50个字符。

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 但是,如果我在正则表达式的第一个标记部分之后放置问号,它将允许诸如,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) (我会说允许它为空白或以上,但是当我要修剪条目时,我会宽容地说空白是可以的)

尝试一下:

^([-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})*)$

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

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