It's generally better to have a whitelist than a blacklist.
Regex has a convenient \w
that, effectively means alphanumeric plus underscore (some variants also add accented chars (á,é,ô,etc) to the list, others don't).
You can invert that by using \W
to mean everything that's not alphanumeric.
So replace \W
with empty string will remove all 'special' characters.
Alternatively, if you do need a different set of characters to alphanumeric, you can use a negated character class: [^abc]
will match everything that is not a
or b
or c
, and [^a-z]
will match everything that is not in the range a,b,c,d...x,y,z
The equivalent to \w
is [A-Za-z0-9_]
and thus \W
is [^A-Za-z0-9_]