Regular Expression for alphanumeric and underscores

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

    Um...question: Does it need to have at least one character or no? Can it be an empty string?

    ^[A-Za-z0-9_]+$
    

    Will do at least one upper or lower case alphanumeric or underscore. If it can be zero length, then just substitute the + for *

    ^[A-Za-z0-9_]*$
    

    Edit:

    If diacritics need to be included (such as cedilla - ç) then you would need to use the word character which does the same as the above, but includes the diacritic characters:

    ^\w+$
    

    Or

    ^\w*$
    

提交回复
热议问题