To handle this situation, you can create a custom function that will escape any special characters in a regular expression, then apply the regex and return the modified string:
String.prototype.escapeChars = function(specialChars) {
for(var i=0; i<specialChars.length; i++) {
if(specialChars[i].match(/\W/)) {
specialChars[i] = '\\'+specialChars[i];
}
}
return this.replace(new RegExp('['+specialChars.join('')+']', 'g'), '');
};
var inputString = "j a\nv\ra?s!c;r.i,p`t:I(s)A{w}e[s]o|m'e\\~=@><&%-/#";
inputString.escapeChars([' ', '\r', '\n', '?', '!', ';', '.', ',', '`', ':', '(', ')', '{', '}', '[', ']', '|', '\'', '\\', '~', '=', '@', '>', '<', '&', '%', '-', '/', '#']);
// result: "JavascriptIsAwesome"