I am trying to strip out all non alpha numeric characters except for comma, dash and single quote. I know how to remove all non words from a string i.e
myString.replace(/\W/g,'');
But how do i do that with the exception of ,
-
and '
? I tried
myString.replace(/\W+[^,]/g,'');
Because i know how to negate using the ^
operator, just having trouble combining the regex.
Any help is appreciated. Thanks.
I am trying to strip out all non alpha numeric characters except for comma, dash and single quote. I know how to remove all non words from a string i.e
myString.replace(/\W/g,'');
But how do i do that with the exception of ,
-
and '
? I tried
myString.replace(/\W+[^,]/g,'');
Because i know how to negate using the ^
operator, just having trouble combining the regex.
Any help is appreciated. Thanks.
Share Improve this question asked Feb 7, 2013 at 22:19 user967420user967420 371 silver badge4 bronze badges 02 Answers
Reset to default 10\w
is the inverse of \W
, so you can just use /[^\w,'-]/
EDIT: in case underscore is also not desired: /[^\w,'-]|_/
The following character class matches a single character that belongs to the class of letters, numbers, comma, dash, and single quote.
[-,'A-Za-z0-9]
The following matches a character that is not one of those:
[^-,'A-Za-z0-9]
So
var stripped = myString.replace(/[^-,'A-Za-z0-9]+/g, '');