Regex to get all alphanumeric fields except comma, dash and single quote

I am trying to cross out all non-alphabetic numeric characters except comma, dash and single quote. I know how to remove all non-words from ie string

myString.replace(/\W/g,'');

      

But how to do this, except for ,

-

and '

? I tried

myString.replace(/\W+[^,]/g,'');

      

Because I know how to deny using an operator ^

just by having regex concatenation problems.

Any help is appreciated. Thank you.

+3


source to share


2 answers


\w

is the inverse \w

, so you can simply use/[^\w,'-]/



EDIT: In case the underscore is also undesirable: /[^\w,'-]|_/

+10


source


The next character class matches a single character belonging 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 the following:



[^-,'A-Za-z0-9]

      

So,

var stripped = myString.replace(/[^-,'A-Za-z0-9]+/g, '');

      

+8


source







All Articles