3

我有一个要忽略的单词列表。但是,当我调用它时,它会替换它的每个实例,即使它在字符串中也是如此。

例如:“he”最终将“the”变成了“t”。

我怎样才能让它自己删除单词?

这是代码:

var commonWords=/and|a|an|has|he|to|was|in|were|are|is|will|as|it|if|
with|at|its|it's|be|by|on|that|from|the|about|again|all|almost|also|although|
always|among|another|any|be|because|been|before|being|between|both|by|can|could|
did|do|does|doesn't|'|done|due|during|each|either|enough|from|had|has|have|having|
here|i|if|into|is|isn't|itself|just|may|might|most|mostly|must|nor|no|neither|nearly|
of|often|on|our|ours|his|hers|he's|he|she|she's|overall|perhaps|quite|rather|really|
regarding|seem|seems|seen|several|should|show|showewd|shown|shows|significant|
significantly|since|so|some|such|than|that|then|their|theirs|there's|therefore|these|
they|this|those|through|thus|to|upon|use|used|using|various|very|was|we|were|what|when|
which|while|with|within|without|would|however|or|for|the|but|etc|yet|/g;

commonWords.ignoreCase;

var w = w.replace(commonWords, '');
4

1 回答 1

8

您不是要替换字符串中的任何实例,而是要替换整个单词。您需要使用锚来查找单词边界。\b

例如...

var commonWords = /\b(and|a|an|has|he|she)\b/g;
于 2013-11-13T22:27:27.613 回答