I want to convert the following string to the provided output.
Input: "\\test\red\bob\fred\new"
Output: "testredbobfrednew"
I've not found any solution that will handle special characters like \r
, \n
, \b
, etc.
Basically I just want to get rid of anything that is not alphanumeric. Here is what I've tried…
Attempt 1: "\\test\red\bob\fred\new".replace(/[_\W]+/g, "");
Output 1: "testedobredew"
Attempt 2: "\\test\red\bob\fred\new".replace(/['`~!@#$%^&*()_|+-=?;:'",.<>\{\}\[\]\\\/]/gi, "");
Output 2: "testedobred [newline] ew"
Attempt 3: "\\test\red\bob\fred\new".replace(/[^a-zA-Z0-9]/, "");
Output 3: "testedobred [newline] ew"
Attempt 4: "\\test\red\bob\fred\new".replace(/[^a-z0-9\s]/gi, '');
Output 4: "testedobred [newline] ew"
One other attempt with multiple steps
function cleanID(id) {
id = id.toUpperCase();
id = id.replace( /\t/ , "T");
id = id.replace( /\n/ , "N");
id = id.replace( /\r/ , "R");
id = id.replace( /\b/ , "B");
id = id.replace( /\f/ , "F");
return id.replace( /[^a-zA-Z0-9]/ , "");
}
with results
Attempt 1: cleanID("\\test\red\bob\fred\new");
Output 1: "BTESTREDOBFREDNEW"
Any help would be appreciated.
Working Solution:
Final Attempt 1: return JSON.stringify("\\test\red\bob\fred\new").replace( /\W/g , '');
Output 1: "testredbobfrednew"
Best Answer
Removing non-alphanumeric chars
The following is the/a correct regex to strip non-alphanumeric chars from an input string:
Note that
\W
is the equivalent of[^0-9a-zA-Z_]
- it includes the underscore character. To also remove underscores use e.g.:The input is malformed
Since the test string contains various escaped chars, which are not alphanumeric, it will remove them.
A backslash in the string needs escaping if it's to be taken literally:
Handling malformed strings
If you're not able to escape the input string correctly (why not?), or it's coming from some kind of untrusted/misconfigured source - you can do something like this:
Note that the json representation of a string includes the quotes:
But they are also removed by the replacement regex.