Javascript regex - no white space at beginning + allow space in the middle
In your 2nd character class, \\s
will match \
and s
, and not \s
. Thus it doesn't matches a whitespace. You should use just \s
there. Also, move the hyphen towards the end, else it will create unintentional range in character class:
^[^-\s][a-zA-Z0-9_\s-]+$
You need to use this regex:
^[^-\s][\w\s-]+$
- Use start anchor
^
- No need to double escape
\s
- Also important is to use hyphen as the first OR last character in the character class.
\w
is same as[a-zA-Z0-9_]