Hugs Not Drugs
JavaSript: Remove all non printable and all non ASCII characters from text 1
According to the ASCII character encoding, there are 95 printable characters in total.
Those characters are in the range
[0x20 to 0x7E] (
[32 to 126] in decimal) and they represent letters, digits, punctuation marks, and a few miscellaneous symbols.
32 in decimal) is the
space character ' ' and
126 in decimal) is the
tilde character '~'.
printable_ASCII_only_string = input_string.replace(/[^ -~]+/g, "");
What the above code does is that it passes the input string through a regular expression which will match all characters out of the printable range and replace them with nothing (hence, delete them).
In case you do not like writing your regular expression with the space character to it, you can re-write the above regular expression using the hex values of the two characters as follows:
printable_ASCII_only_string = input_string.replace(/[^\x20-\x7E]+/g, "");