How I should Remove the Special Characters from a String in JavaScript?
I need to create a function that takes a string, removes all "special" characters (e.g. !, @, #, $, %, ^, &, \, *, (, )) and returns the new string. The only non-alphanumeric characters allowed are dashes -, underscores _ and spaces.
1 Answer
This function returns exactly what is required, using regExp making life ease
function removeSpecialCharacters(str) {
var replaced = str.replace(/[^a-zA-Z0-9-_\s]/g, '');
return replaced;
}
Example
removeSpecialCharacters('world$$ here') // "world here"
answer Link