如何测试字符串是否包含所选关键字?
例如
var keywords = 'small, big, large'
var string = 'big brown bear';
function wordInString(string, keywords){
return new RegExp( '\\b' + keywords + '\\b', 'i').test(string);
}
以上仅适用于单个单词,我需要能够测试多个单词,并且完全匹配。
答案 0 :(得分:5)
将字符串拆分为单词,并使用关键字数组
function wordInString(string, keywords) {
return string.split(/\b/).some(Array.prototype.includes.bind(keywords));
}
var keywords = ['small', 'big', 'large'];
var result1 = wordInString('big brown bear', keywords); // true
var result2 = wordInString('great brown bear', keywords); // false
var result3 = wordInString('Big brown bear', keywords); // false
console.log(result1, result2, result3);

ES5(跨浏览器)版本
function wordInString(string, keywords) {
return string.split(/\b/).filter(function(w) {
return keywords.indexOf(w) !== -1;
}).length > 0;
}
返回所有单词
function wordInString(string, keywords) {
return keywords.filter(function(x) { return this.includes(x) }, string.split(/\b/));
}
var keywords = ['small', 'big', 'large'];
var result1 = wordInString('big brown bear large', keywords); // ["big", "large"]
var result2 = wordInString('great brown bear', keywords); // []
var result3 = wordInString('Big brown bear', keywords); // []
console.log(result1);
console.log(result2);
console.log(result3);

返回第一个匹配的单词或空字符串
function wordInString(string, keywords) {
var r = "";
string.split(/\b/).some( x => {
return r = keywords.includes(x) ? x : "";
})
return r;
}
var keywords = ['small', 'big', 'large'];
var result1 = wordInString('big brown bear large', keywords); // "big"
var result2 = wordInString('great brown bear', keywords); // ""
var result3 = wordInString('Big brown bear', keywords); // ""
console.log(result1);
console.log(result2);
console.log(result3);

答案 1 :(得分:1)
使用一系列关键字,并循环使用它们:
var keywords = ['small', 'big', 'large'];
console.log( wordInString("big brown bear", keywords) ); // true
console.log( wordInString("it's small!", keywords) ); // true
console.log( wordInString("it's larger than the other", keywords) );// false
console.log( wordInString("it's black and red", keywords) ); // false
function wordInString(string, keywords){
for(var i=0; i<keywords.length; i++){
if(new RegExp( '\\b' + keywords[i] + '\\b', 'i').test(string)){
return true;
}
}
return false;
}
&#13;
答案 2 :(得分:1)
如果找不到,则返回匹配的单词或预设字符串。
function wordInString(string, keywords) {
return string.split(/\b/).filter(word => keywords.some(w => w === word))[0] || 'empty';
}
var keywords = ['small', 'big', 'large'];
var result1 = wordInString('big brown bear', keywords); // big
var result2 = wordInString('tiny bear', keywords); // empty
var result3 = wordInString('huge hairy bear', keywords); // empty
console.log(result1, result2, result3);
答案 3 :(得分:0)
以下是使用map
,reduce
函数和logical operators
的不同解决方案。
var keywords = 'small, big, large'
var test1 = testString('big brown bear', keywords); //big
var test2 = testString('great brown bear', keywords); // empty
var test3 = testString('Big brown bear', keywords); // empty
function wordInString(string, keywords){
return new RegExp( '\\b' + keywords + '\\b').test(string);
}
function testString(string,keywords){
var word='empty';
var result=keywords.split(',').map(function(item){
if(wordInString(string,item.trim())==true)
word=item.trim();
return wordInString(string,item.trim());
}).reduce(function(curr,prev){
return curr || prev;
});
return word;
}
console.log(test1)
console.log(test2)
console.log(test3)
&#13;
答案 4 :(得分:0)
拆分关键字,然后在字符串上搜索indexOf
个关键字。
var keywords = 'small, big, large'
var string = 'big brown bear';
function wordInString(string, keywords) {
return keywords.split(',').some(function(keyword) {
return string.indexOf(keyword.trim()) == -1 ? false : true
});
//return new RegExp('\\b' + keywords + '\\b', 'i').test(string);
}
console.log(wordInString(string, keywords))
console.log(wordInString(string, "xyz, abc"))
console.log(wordInString("'a large bear'", "large, none"))