My JavaScript is quite rusty so any help with this would be great. I have a requirement to detect non printable characters (control characters like SOH, BS etc) as well exte
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, "");