Regex pattern including all special characters

后端 未结 18 1596
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-02 09:18

I want to write a simple regular expression to check if in given string exist any special character. My regex works but I don\'t know why it also includes all numbers, so wh

18条回答
  •  长情又很酷
    2020-12-02 09:40

    If you only rely on ASCII characters, you can rely on using the hex ranges on the ASCII table. Here is a regex that will grab all special characters in the range of 33-47, 58-64, 91-96, 123-126

    [\x21-\x2F\x3A-\x40\x5B-\x60\x7B-\x7E]
    

    However you can think of special characters as not normal characters. If we take that approach, you can simply do this

    ^[A-Za-z0-9\s]+
    

    Hower this will not catch _ ^ and probably others.

提交回复
热议问题