RegEx for Javascript to allow only alphanumeric

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

    ^\s*([0-9a-zA-Z]*)\s*$
    

    or, if you want a minimum of one character:

    ^\s*([0-9a-zA-Z]+)\s*$
    

    Square brackets indicate a set of characters. ^ is start of input. $ is end of input (or newline, depending on your options). \s is whitespace.

    The whitespace before and after is optional.

    The parentheses are the grouping operator to allow you to extract the information you want.

    EDIT: removed my erroneous use of the \w character set.

提交回复
热议问题