RegEx for Javascript to allow only alphanumeric

前端 未结 18 1438
轻奢々
轻奢々 2020-11-22 15:13

I need to find a reg ex that only allows alphanumeric. So far, everyone I try only works if the string is alphanumeric, meaning contains both a letter and a number. I just w

18条回答
  •  长发绾君心
    2020-11-22 15:38

    Use the word character class. The following is equivalent to a ^[a-zA-Z0-9_]+$:

    ^\w+$
    

    Explanation:

    • ^ start of string
    • \w any word character (A-Z, a-z, 0-9, _).
    • $ end of string

    Use /[^\w]|_/g if you don't want to match the underscore.

提交回复
热议问题