[英]RegEx to allow letters numbers and spaces but not two spaces in a row
Hello I have a RegEx that allows up to 18 characters with numbers, letters and spaces only.您好,我有一个 RegEx,最多允许 18 个字符,仅包含数字、字母和空格。
How can I modify this to check if the string is starting with a space, or has two spaces in a row?如何修改它以检查字符串是否以空格开头,或者是否连续有两个空格?
/^[0-9A-Za-z\s]{1,18}$/
I am using this regex in both native javascript (not jquery) and PHP.我在本机 javascript(不是 jquery)和 PHP 中使用这个正则表达式。
您可以使用否定前瞻来检查字符串是否以空格开头或是否有 2 个或更多连续空格,例如:
^(?!.*\s{2,})(?!^ )[0-9A-Za-z\s]{1,18}$
(?:(?![ ]{2}).)+
use this regex.使用这个正则表达式。 This may solve your problem.
这可能会解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.