问题
I am struggling to make a javascript regex to satisfy the following:
- The first character has to be alphabetical (
[a-zA-Z]
) - The rest can be any letters, any numbers, hyphen, dot, underscore and spaces
- BUT no consecutive spaces, e.g: two or more spaces in a row
- The length has to be between 3 and 25 (inclusive)
So here is what I found Regex:
/^[a-z][\s\w.-]{3,24}$/i
My current regex works, but won't be able to test whether the user has written consecutive spaces. How can I test for that?
回答1:
This should work for you:
/^[a-z](?!.* {2})[ \w.-]{2,24}$/gmi
RegEx Demo
来源:https://stackoverflow.com/questions/28751997/regex-to-match-a-username-with-no-consecutive-spaces