您正在尋找這樣的事情:
var commonWords=["she", "he", "him", "liked", "i", "a", "an", "are"];
var regstr = "\\b(" + commonWords.join("|") + ")\\b";
//regex is \b(she|he|him|liked|i|a|an|are)\b
var regex = new RegExp(regstr, "ig");
var str = 'She met him where he liked to eat "the best" cheese pizza.';
console.log(str.replace(regex, ""));
輸出
met where to eat "the best" cheese pizza.
split
版本:
var commonWords=["she", "he", "him", "liked", "i", "a", "an", "are"];
var regstr = "\\b(?:" + commonWords.join("|") + ")\\b";
var regex = new RegExp(regstr, "ig");
var str = 'She met him where he liked to eat "the best" cheese pizza.';
var arr = str.split(regex);
console.log(arr);// ["", " met ", " where ", " ", " to eat "the best" cheese pizza."]
for(var i = 0; i < arr.length; i++)
if(arr[i].match(/^\s*$/)) //remove empty strings and strings with only spaces.
arr.splice(i--, 1);
else
arr[i] = arr[i].replace(/^\s+|\s+$/g, ""); //trim spaces from beginning and end
console.log(arr);// ["met", "where", "to eat "the best" cheese pizza."]
console.log(arr.join(", "));// met, where, to eat "the best" cheese pizza.
的反應應該是:''滿足,其中,吃,最好,奶酪pizza''。 「喜歡」位於commonWords列表中。 – 2010-06-29 10:59:45
謝謝!如此真實。 – 2010-06-29 21:19:16