如果我有一个字符串,其中有任何类型的非字母数字字符:

"This., -/ is #! an $ % ^ & * example ;: {} of a = -_ string with `~)() punctuation"

我如何在JavaScript中得到一个没有标点符号的版本:

"This is an example of a string with punctuation"

当前回答

如果你想从任何字符串中删除标点符号,你应该使用P Unicode类。

但是,由于JavaScript RegEx不接受类,您可以尝试这个RegEx,它应该匹配所有的标点符号。它匹配以下类别:Pc Pd Pe Pf Pi Po Ps Sc Sk Sm So一般标点符号补充标点符号CJKSymbolsAndPunctuation cuneiformnumber和punctuation。

我使用这个在线工具创建了它,它专门为JavaScript生成正则表达式。 这是实现你目标的代码:

var punctuationRegEx = /[!-/:-@[-`{-~¡-©«-¬®-±´¶-¸»¿×÷˂-˅˒-˟˥-˫˭˯-˿͵;΄-΅·϶҂՚-՟։-֊־׀׃׆׳-״؆-؏؛؞-؟٪-٭۔۩۽-۾܀-܍߶-߹।-॥॰৲-৳৺૱୰௳-௺౿ೱ-ೲ൹෴฿๏๚-๛༁-༗༚-༟༴༶༸༺-༽྅྾-࿅࿇-࿌࿎-࿔၊-၏႞-႟჻፠-፨᎐-᎙᙭-᙮᚛-᚜᛫-᛭᜵-᜶។-៖៘-៛᠀-᠊᥀᥄-᥅᧞-᧿᨞-᨟᭚-᭪᭴-᭼᰻-᰿᱾-᱿᾽᾿-῁῍-῏῝-῟῭-`´-῾\u2000-\u206e⁺-⁾₊-₎₠-₵℀-℁℃-℆℈-℉℔№-℘℞-℣℥℧℩℮℺-℻⅀-⅄⅊-⅍⅏←-⏧␀-␦⑀-⑊⒜-ⓩ─-⚝⚠-⚼⛀-⛃✁-✄✆-✉✌-✧✩-❋❍❏-❒❖❘-❞❡-❵➔➘-➯➱-➾⟀-⟊⟌⟐-⭌⭐-⭔⳥-⳪⳹-⳼⳾-⳿⸀-\u2e7e⺀-⺙⺛-⻳⼀-⿕⿰-⿻\u3000-〿゛-゜゠・㆐-㆑㆖-㆟㇀-㇣㈀-㈞㈪-㉃㉐㉠-㉿㊊-㊰㋀-㋾㌀-㏿䷀-䷿꒐-꓆꘍-꘏꙳꙾꜀-꜖꜠-꜡꞉-꞊꠨-꠫꡴-꡷꣎-꣏꤮-꤯꥟꩜-꩟﬩﴾-﴿﷼-﷽︐-︙︰-﹒﹔-﹦﹨-﹫!-/:-@[-`{-・¢-₩│-○-�]|\ud800[\udd00-\udd02\udd37-\udd3f\udd79-\udd89\udd90-\udd9b\uddd0-\uddfc\udf9f\udfd0]|\ud802[\udd1f\udd3f\ude50-\ude58]|\ud809[\udc00-\udc7e]|\ud834[\udc00-\udcf5\udd00-\udd26\udd29-\udd64\udd6a-\udd6c\udd83-\udd84\udd8c-\udda9\uddae-\udddd\ude00-\ude41\ude45\udf00-\udf56]|\ud835[\udec1\udedb\udefb\udf15\udf35\udf4f\udf6f\udf89\udfa9\udfc3]|\ud83c[\udc00-\udc2b\udc30-\udc93]/g; var string = "This., -/ is #! an $ % ^ & * example ;: {} of a = -_ string with `~)() punctuation"; var newString = string.replace(punctuationRegEx, '').replace(/(\s){2,}/g, '$1'); console.log(newString)

其他回答

下面是US-ASCII的标准标点符号:!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

对于Unicode标点符号(如大引号、em-dash等),可以很容易地匹配特定的块范围。一般标点符号块是\u2000-\u206F,补充标点符号块是\u2E00-\u2E7F。

放在一起,并正确转义,您将得到以下RegExp:

/[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,\-.\/:;<=>?@\[\]^_`{|}~]/

这应该可以匹配您遇到的任何标点符号。那么,来回答最初的问题:

var punctRE = /[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,\-.\/:;<=>?@\[\]^_`{|}~]/g;
var spaceRE = /\s+/g;
var str = "This, -/ is #! an $ % ^ & * example ;: {} of a = -_ string with `~)() punctuation";
str.replace(punctRE, '').replace(spaceRE, ' ');

>> "This is an example of a string with punctuation"

US-ASCII来源:http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#posix

Unicode来源:http://kourge.net/projects/regexp-unicode-block

我遇到过同样的问题,这个解决方案很管用,而且可读性很强:

var sentence = "This., -/ is #! an $ % ^ & * example ;: {} of a = -_ string with `~)() punctuation";
var newSen = sentence.match(/[^_\W]+/g).join(' ');
console.log(newSen);

结果:

"This is an example of a string with punctuation"

诀窍是创建一个负集合。这意味着它匹配任何不在集合内的东西,即[^abc] -不是a, b或c

\W是任何非单词,所以[^\W]+将否定任何非单词char。

通过添加_(下划线),你也可以否定它。

让它全局应用/g,然后你可以通过它运行任何字符串,并清除标点符号:

/[^_\W]+/g

又漂亮又干净;)

如果您想从字符串中删除特定的标点符号,最好明确地删除您想要的标点符号,例如

replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"")

执行上述操作仍然不会返回您指定的字符串。如果您想要删除由于删除疯狂的标点符号而留下的任何额外的空格,那么您将需要执行以下操作

replace(/\s{2,}/g," ");

完整的例子:

var s = "This., -/ is #! an $ % ^ & * example ;: {} of a = -_ string with `~)() punctuation";
var punctuationless = s.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"");
var finalString = punctuationless.replace(/\s{2,}/g," ");

在firebug控制台中运行代码的结果:

/[^A-Za-z0-9\s]/g应该匹配所有的标点符号,但要保留空格。 因此,如果需要的话,可以使用.replace(/\s{2,}/g, " ")替换额外的空格。您可以在http://rubular.com/中测试正则表达式

.replace(/[^A-Za-z0-9\s]/g,"").replace(/\s{2,}/g, " ")

更新:只有当输入是ANSI英语时才会工作。

它很简单,只是替换字符而不是单词:

.replace(/[^\w]/g, ' ')