How to limit string size for this regular expression?
/^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$/
I just need to add the quantifier {3,16}.
{3,16}
Sprinkle in some positive lookahead to test for the total length of the string by adding
(?=.{3,16}$)
at the start of the regex. The final regex is then:
/^(?=.{3,16}$)[a-z][a-z0-9]*(?:_[a-z0-9]+)*$/
2.1m questions
2.1m answers
60 comments
57.0k users