我正在尝试从字符串中过滤掉任何常用字词。我在互联网上找到了这个代码,它看起来会很完美,但是如何修改它却不会返回带有','的字符串?
当前代码:
function getUncommon(cquerySearch, filterCommonWords) {
var wordArr = sentence.match(/\w+/g),
commonObj = {},
uncommonArr = [],
word, i;
common = common.split(',');
for ( i = 0; i < common.length; i++ ) {
commonObj[ common[i].trim() ] = true;
}
for ( i = 0; i < wordArr.length; i++ ) {
word = wordArr[i].trim().toLowerCase();
if ( !commonObj[word] ) {
uncommonArr.push(word);
}
}
return uncommonArr;
}
这将返回类似uncommonArr = 'Return, String, Would, Go, Here'
的数组。谢谢你的帮助!
答案 0 :(得分:4)
Array.toString
的默认行为是加入,
。只需指定自定义连接字符串:
uncommonArr.join(' '); // Join with a space, for example