The accepted answer on the question proposed duplicate doesn't cover all the punctuation characters in ASCII range. (The comment on the accepted answer does, though).
A better way to write this regex is to use put the characters into a character class.
/[~`!@#$%^&*(){}\[\];:"'<,.>?\/\\|_+=-]/g
In a character class, to match the literal characters:
^
does not need escaping, unless it is at the beginning of the character class.
-
should be placed at the beginning of the character class (after the ^
in a negated character class) or at the end of a character class.
]
has to be escaped to be specified as literal character. [
does not need to be escaped (but I escape it anyway, as a habit, since some language requires [
to be escaped inside character class).
$
, *
, +
, ?
, (
, )
, {
, }
, |
, .
loses their special meaning inside character class.
In RegExp literal, /
has to be escaped.
In RegExp, since \
is the escape character, if you want to specify a literal \
, you need to escape it \\
.