问题
Hello I have a RegEx that allows up to 18 characters with numbers, letters and spaces only.
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.
回答1:
You can use a negative look-ahead to check if the string starts with a space or has 2 or more consecutive spaces, something like:
^(?!.*\s{2,})(?!^ )[0-9A-Za-z\s]{1,18}$
回答2:
(?:(?![ ]{2}).)+
use this regex. This may solve your problem.
来源:https://stackoverflow.com/questions/36902258/regex-to-allow-letters-numbers-and-spaces-but-not-two-spaces-in-a-row