[英]Allow alpha characters, space, dash, apostrophe and length <= 50
I'm trying to write regular expression that allows alpha characters, space, dash, apostrophe and length 50. So far I have this: 我正在尝试编写允许字母字符,空格,破折号,撇号和长度为50的正则表达式。到目前为止,我有以下内容:
/^([A-Za-z\s].{1,50})$/
I'm not sure where I should place the code for dash and apostrophe. 我不确定应该在哪里放置破折号和撇号的代码。 If anyone can help pleases let me know. 如果有人可以帮助,请告诉我。 Thanks. 谢谢。
You need 你需要
/^[A-Za-z '-]{1,50}$/
or 要么
/^[A-Za-z\s'-]{1,50}$/
When you use \\s
instead of a space, you will allow any whitespace. 当使用\\s
代替空格时,将允许任何空格。
The apostrophe can be placed anywhere inside the character class (so as not to ruin the ranges), and the hyphen at the start/end of the character class does not need to be escaped. 撇号可以放置在字符类内部的任何位置(以免破坏范围),并且无需转义字符类开头/结尾的连字符。
If you use {1,50}
limiting quantifier , it means you allow 1 to 50 chars of the type specified in the character class. 如果使用{1,50}
限制量词 ,则表示您允许1到50个字符类中指定类型的字符。 If you allow exactly 50 chars, use /^[A-Za-z\\s'-]{50}$/
. 如果您恰好允许50个字符,请使用/^[A-Za-z\\s'-]{50}$/
。 If you use just +
instead, you will allow 1 or more characters. 如果仅使用+
,则允许1个或多个字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.