To target characters that are not part of the printable basic ASCII range, you can use this simple regex:
[^ -~]+
Explanation: in the first 128 characters of the ASCII table, the printable range starts with the space character and ends with a tilde. These are the characters you want to keep. That range is expressed with [ -~]
, and the characters not in that range are expressed with [^ -~]
. These are the ones we want to replace. Therefore:
result = string.replace(/[^ -~]+/g, "");