Regular Expression for alphanumeric and underscores

前端 未结 20 970
北荒
北荒 2020-11-22 10:01

I would like to have a regular expression that checks if a string contains only upper and lowercase letters, numbers, and underscores.

20条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 10:36

    use lookaheads to do the "at least one" stuff. Trust me it's much easier.

    Here's an example that would require 1-10 characters, containing at least one digit and one letter:

    ^(?=.*\d)(?=.*[A-Za-z])[A-Za-z0-9]{1,10}$
    

    NOTE: could have used \w but then ECMA/Unicode considerations come into play increasing the character coverage of the \w "word character".

提交回复
热议问题