正则表达式获取除逗号,短划线和单引号之外的所有字母数字字段 [英] Regex to get all alpha numeric fields except for comma, dash and single quote

查看:217
本文介绍了正则表达式获取除逗号,短划线和单引号之外的所有字母数字字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除除逗号,短划线和单引号之外的所有非字母数字字符。我知道如何从字符串中删除所有非单词,即

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.

推荐答案

\ w \ W ,所以你可以使用 / [^ \w,' - ] /

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

编辑:如果不想要下划线: / [^ \ w,' - ] | _ /

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

这篇关于正则表达式获取除逗号,短划线和单引号之外的所有字母数字字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆