I need to restrict a string to only allow letters, numbers, hyphens, ampersands, apostrophes and single spaces.
我需要将字符串限制为仅允许字母,数字,连字符,&符号,撇号和单个空格。
From a bit of searching I've got this so far:
从一点点搜索到目前为止我已经得到了这个:
^[A-Za-z0-9-'&\s]{1,}$
But this allows for double spaces. How do I write the regular expression so that it only allows single spaces (there might not be any at all)?
但这允许双倍空间。如何编写正则表达式以便它只允许单个空格(可能根本没有)?
2 个解决方案
#1
7
Match any of the other allowed values, followed by an optional single space:
匹配任何其他允许的值,后跟可选的单个空格:
^\s?([A-Za-z0-9-'&]\s?){1,}$
(I also added an optional one at the start, if that's allowed)
(如果允许的话,我还在开头添加了一个可选的)
#2
1
Try this
^([A-Za-z0-9-'&]+\s?)+$
#1
7
Match any of the other allowed values, followed by an optional single space:
匹配任何其他允许的值,后跟可选的单个空格:
^\s?([A-Za-z0-9-'&]\s?){1,}$
(I also added an optional one at the start, if that's allowed)
(如果允许的话,我还在开头添加了一个可选的)
#2
1
Try this
^([A-Za-z0-9-'&]+\s?)+$