忽略单词列表

时间:2013-11-13 22:20:05

标签: javascript regex variables replace

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

例如:“他”最终将“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, '');

1 个答案:

答案 0 :(得分:8)

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

例如......

var commonWords = /\b(and|a|an|has|he|she)\b/g;