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 に答える