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

后端 未结 6 1851
无人共我
无人共我 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:48

    Answer

    /[\W\S_]/
    

    Explanation

    This creates a character class removing the word characters, space characters, and adding back the underscore character (as underscore is a "word" character). All that is left is the special characters. Capital letters represent the negation of their lowercase counterparts.

    \W will select all non "word" characters equivalent to [^a-zA-Z0-9_]
    \S will select all non "whitespace" characters equivalent to [ \t\n\r\f\v]
    _ will select "_" because we negate it when using the \W and need to add it back in

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