What regex can I use to match only letters, numbers, and one space between each word?

后端 未结 6 1026
执笔经年
执笔经年 2021-02-01 05:24

How can I create a regex expression that will match only letters and numbers, and one space between each word?

Good Examples:



        
6条回答
  •  佛祖请我去吃肉
    2021-02-01 05:50

    Most regex implementations support named character classes:

    ^[[:alnum:]]+( [[:alnum:]]+)*$
    

    You could be clever though a little less clear and simplify this to:

    ^([[:alnum:]]+ ?)*$
    

    FYI, the second one allows a spurious space character at the end of the string. If you don't want that stick with the first regex.

    Also as other posters said, if [[:alnum:]] doesn't work for you then you can use [A-Za-z0-9] instead.

提交回复
热议问题