忽略词列表

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, '');
1个回答

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

例如...

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接