0
我想寫一個函數,從字符串中刪除特定的單詞。JavaScript的正則表達式 - 替換字符串停用詞
下面的代碼工作正常,直到句子的最後一個單詞,因爲它沒有跟着我的正則表達式尋找的空間。
我怎樣才能捕捉到最後一個沒有空格的單詞?
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");
當然!謝謝! – Ben 2013-03-16 17:33:52