Skip to content Skip to sidebar Skip to footer

Regex To Get All Alpha Numeric Fields Except For Comma, Dash And Single Quote

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,'');

Solution 1:

\w is the inverse of \W, so you can just use /[^\w,'-]/

EDIT: in case underscore is also not desired: /[^\w,'-]|_/

Solution 2:

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, '');

Post a Comment for "Regex To Get All Alpha Numeric Fields Except For Comma, Dash And Single Quote"