This is generic for the bigger-picture approach, say you wanted to clean out (or select) any symbols from a string.
A cleaner approach will be to select anything that is not alphanumeric, which by elimination must be a symbol, simply by using /\W/
, see [1]. The regex will be
let re = /\W/g
// for example, given a string and you would like to
// clean out any non-alphanumerics
// remember this will include the spaces
let s = "he$$llo# worl??d!"
s = s.replace(re, '') // "helloworld"
However, if you need to exclude all non-alphanumerics except a few, say "space" from our previous example. You can use the [^ ...]
(hat) pattern.
let re = /[^ \w]/g // match everything else except space and \w (alphanumeric)
let s = "he$$llo# worl??d!"
s = s.replace(re, '') // "hello world"
References:
[1] https://regexone.com/