Several conditions for the JavaScript.includes () method

just wondering if there is a way to add several conditions to the .includes method, for example:

var value = str.includes("hello", "hi", "howdy"); 

imagine the commas "or" (he asks now whether the string contains hello hi or howdy, so only if one and only one of the conditions is true.

Is there any way to do this?

+39
source share
7 answers

This should work, even if one and only one of the conditions is true:

 var str = "bonjour le monde vive le javascript"; var arr = ['bonjour','europe', 'c++']; function contains(target, pattern){ var value = 0; pattern.forEach(function(word){ value = value + target.includes(word); }); return (value === 1) } console.log(contains(str, arr)); 
+14
source

You can use the .some method here .

The some() method checks to see if at least one element in the array has passed the test implemented by the provided function.

 // test cases var str1 = 'hi, how do you do?'; var str2 = 'regular string'; // does the test strings contains this terms? var conditions = ["hello", "hi", "howdy"]; // run the tests agains every element in the array var test1 = conditions.some(el => str1.includes(el)); var test2 = conditions.some(el => str2.includes(el)); // display results console.log(str1, ' ===> ', test1); console.log(str2, ' ===> ', test2); 
+101
source

With include includes() , no, but you can achieve the same with REGEX via test() :

 var value = /hello|hi|howdy/.test(str); 

Or, if the words come from a dynamic source:

 var words = array('hello', 'hi', 'howdy'); var value = new RegExp(words.join('|')).test(str); 

The REGEX approach is a better idea because it allows you to match words as actual words, rather than substrings of other words. You just need the word boundary marker \b , so:

 var str = 'hilly'; var value = str.includes('hi'); //true, even though the word 'hi' isn't found var value = /\bhi\b/.test(str); //false - 'hi' appears but not as its own word 
+16
source

Not the best answer and not the cleanest, but I think this is more appropriate. For example, if you want to use the same filters for all your checks. Actually .filter() works with an array and returns a filtered array (which I find easier to use).

 var str1 = 'hi, how do you do?'; var str2 = 'regular string'; var conditions = ["hello", "hi", "howdy"]; // Solve the problem var res1 = [str1].filter(data => data.includes(conditions[0]) || data.includes(conditions[1]) || data.includes(conditions[2])); var res2 = [str2].filter(data => data.includes(conditions[0]) || data.includes(conditions[1]) || data.includes(conditions[2])); console.log(res1); // ["hi, how do you do?"] console.log(res2); // [] // More useful in this case var text = [str1, str2, "hello world"]; // Apply some filters on data var res3 = text.filter(data => data.includes(conditions[0]) && data.includes(conditions[2])); // You may use again the same filters for a different check var res4 = text.filter(data => data.includes(conditions[0]) || data.includes(conditions[1])); console.log(res3); // [] console.log(res4); // ["hi, how do you do?", "hello world"] 
0
source

Here's a controversial option :

 String.prototype.includesOneOf = function(arrayOfStrings) { if(!Array.isArray(arrayOfStrings)) { throw new Error('includesOneOf only accepts an array') } return arrayOfStrings.some(str => this.includes(str)) } 

Lets you do things like:

 'Hi, hope you like this option'.toLowerCase().includesOneOf(["hello", "hi", "howdy"]) // True 
0
source

This can be done using some / every Array and RegEx methods.

To check if ALL words from a list (array) are present in a string:

 const multiSearchAnd = (text, searchWords) => ( searchWords.every((el) => { return text.match(new RegExp(el,"i")) }) ) multiSearchAnd("Chelsey Dietrich Engineer 2018-12-11 Hire", ["cle", "hire"]) //returns false multiSearchAnd("Chelsey Dietrich Engineer 2018-12-11 Hire", ["che", "hire"]) //returns true 

To check if there are ANY words from a list (array) in a string:

 const multiSearchOr = (text, searchWords) => ( searchWords.some((el) => { return text.match(new RegExp(el,"i")) }) ) multiSearchOr("Chelsey Dietrich Engineer 2018-12-11 Hire", ["che", "hire"]) //returns true multiSearchOr("Chelsey Dietrich Engineer 2018-12-11 Hire", ["aaa", "hire"]) //returns true multiSearchOr("Chelsey Dietrich Engineer 2018-12-11 Hire", ["che", "zzzz"]) //returns true multiSearchOr("Chelsey Dietrich Engineer 2018-12-11 Hire", ["aaa", "1111"]) //returns false 
0
source

Other!

 let result const givenStr = 'A, X' //values separated by comma or space. const allowed = ['A', 'B'] const given = givenStr.split(/[\s,]+/).filter(v => v) console.log('given (array):', given) // given contains none or only allowed values: result = given.reduce((acc, val) => { return acc && allowed.includes(val) }, true) console.log('given contains none or only allowed values:', result) // given contains at least one allowed value: result = given.reduce((acc, val) => { return acc || allowed.includes(val) }, false) console.log('given contains at least one allowed value:', result) 

0
source

All Articles