Regex to Match Symbols: !$%^&*()_+|~-=`{}[]:";'<>?,./
The regular expression for this is really simple. Just use a character class. The hyphen is a special character in character classes, so it needs to be first:
/[-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]/
You also need to escape the other regular expression metacharacters.
Edit: The hyphen is special because it can be used to represent a range of characters. This same character class can be simplified with ranges to this:
/[$-/:-?{-~!"^_`\[\]]/
There are three ranges. '$' to '/', ':' to '?', and '{' to '~'. the last string of characters can't be represented more simply with a range: !"^_`[].
Use an ACSII table to find ranges for character classes.
The most simple and shortest way to accomplish this:
/[^\p{L}\d\s@#]/u
Explanation
[^...]
Match a single character not present in the list below
\p{L}
=> matches any kind of letter from any language\d
=> matches a digit zero through nine\s
=> matches any kind of invisible character@#
=>@
and#
characters
Don't forget to pass the u
(unicode) flag.
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