Regular Expression for alphanumeric and underscores

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

    In Computer Science, an Alphanumeric value often means the first character is not a number but is an alphabet or underscore. Thereafter the character can be 0-9, A-Z, a-z, or underscore (_).

    Here is how you would do that:

    Tested under php:

    $regex = '/^[A-Za-z_][A-Za-z\d_]*$/'
    

    or take this

    ^[A-Za-z_][A-Za-z\d_]*$
    

    and place it in your development language.

提交回复
热议问题