简体   繁体   中英

JavaScript/jQuery - Check if string contains one of the keywords

How to test if string contains on of the selected keywords?

For example

var keywords = 'small, big, large'
var string = 'big brown bear';

function wordInString(string, keywords){
  return new RegExp( '\\b' + keywords + '\\b', 'i').test(string);
}

The above only works for a single word, I need to be able to test multiple words, and exact match.

Split the string into words, and use an array of keywords

 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 (cross-browser) version

function wordInString(string, keywords) {
    return string.split(/\b/).filter(function(w) {
        return keywords.indexOf(w) !== -1;
    }).length > 0;
}

To return all the words

 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); 

To return the first matching word or an empty string

 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); 

Use an array of keywords, and loop through them:

 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; } 

Returns matched word or preset string if not found.

 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); 

Here is a different solution using map , reduce functions and 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) 

Split the keywords and then search indexOf of keywords on the string.

 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")) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM