最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regex to get all alpha numeric fields except for comma, dash and single quote - Stack Overflow

programmeradmin1浏览0评论

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 0
Add a comment  | 

2 Answers 2

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论