Regex to Match Symbols: !$%^&*()_+|~-=`{}[]:";'<>?,./

后端 未结 6 1838
无人共我
无人共我 2020-11-28 19:20

I\'m trying to create a Regex test in JavaScript that will test a string to contain any of these characters:

!$%^&am         


        
6条回答
  •  遥遥无期
    2020-11-28 19:36

    A simple way to achieve this is the negative set [^\w\s]. This essentially catches:

    • Anything that is not an alphanumeric character (letters and numbers)
    • Anything that is not a space, tab, or line break (collectively referred to as whitespace)

    For some reason [\W\S] does not work the same way, it doesn't do any filtering. A comment by Zael on one of the answers provides something of an explanation.

?,./" id="ans_title" name="title">
提交回复
热议问题