RegEx for Javascript to allow only alphanumeric

前端 未结 18 1533
轻奢々
轻奢々 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:32

    Even better than Gayan Dissanayake pointed out.

    /^[-\w\s]+$/
    

    Now ^[a-zA-Z0-9]+$ can be represented as ^\w+$

    You may want to use \s instead of space. Note that \s takes care of whitespace and not only one space character.

提交回复
热议问题