I have the following code:
ttt = 'hello---,,..there';
tt2 = strip(ttt);
alert(tt2);
function strip(str){
return str.replace(/[^a-zA-Z0-9 ,.-_]/g, '');
}
The alert gives hello,,..there
I would expect it to give hello---,,..there as all the characters, including hyphens, are specified as exceptions in the replacement function.
What am I doing wrong?
Advertisement
Answer
Escape the hyphen:
'hello---,,..there'.replace(/[^a-zA-Z0-9 ,.-_]/g, ''); // => "hello---,,..there"