文字列から特定の単語を削除する関数を作成しようとしています。
以下のコードは、私の正規表現が検索するスペースが後に続かないため、文の最後の単語まで問題なく機能します。
スペースが続かない最後の単語をキャプチャするにはどうすればよいですか?
function stopwords(input) {
var stop_words = new Array('a', 'about', 'above', 'across');
console.log('IN: ' + input);
stop_words.forEach(function(item) {
var reg = new RegExp(item +'\\s','gi')
input = input.replace(reg, "");
});
console.log('OUT: ' + input);
}
stopwords( "this is a test string mentioning the word across and a about");