Regular Expression for alphanumeric and underscores

前端 未结 20 976
北荒
北荒 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:52

    There's a lot of verbosity in here, and I'm deeply against it, so, my conclusive answer would be:

    /^\w+$/
    

    \w is equivalent to [A-Za-z0-9_], which is pretty much what you want. (unless we introduce unicode to the mix)

    Using the + quantifier you'll match one or more characters. If you want to accept an empty string too, use * instead.

提交回复
热议问题