简体   繁体   中英

How do I check for two words on a string using include()?

I've tried &&, ||, and some other comparison methods between the words. Currently, it's like this:

if(str.includes("a", "b") {
console.log(a and b received)
}

And the problem is that currently, it logs that a and b received even if the string only has a, when I only want it to log that if the string has both a and b. Does anyone know what's the proper command for this? (ps I'm a total noob to js and I have no idea am I even supposed to use include() for something like this)

if(str.includes("a") && str.includes("b")) {
console.log(a and b received)
}

should do it.

There are various method:

Method 1 - using includes

 str.includes("a") && str.includes("b")

Method 2 - using indexOf

str.indexOf("a") > -1 && str.indexOf("b") > -1

Method 3 - using contains

str.contains("a") && str.contains("b")

Method 4 - using search

str.search('a') > -1 && str.search('a') > -1

Method 5 - using match

str.match('a') && str.match('b')

Method 6 - using RegExp

RegExp('a').test(str) && RegExp('a').test(str)
if(str.includes("a") && str.includes("b")) {
  console.log("a and b received")
}

You can also read from the docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes , that includes function receives only a valueToFind argument (plus an optional fromIndex One), and that's the reason why your approach doesn't work.

if(str.includes('word1') && str.includes('word2')){
  console.log("word1 and word2 received");
}

A canonical solution is to take a quantifier, like Array#every and take the result.

The advantage is to have the option to add more values to the array for checking isntead of adding another condition for a check.

if (["a", "b"].every(part => str.includes(part)) {
    console.log('str contains a and b');
}

As you can read on the docs the .includes() method determines whether one string may be found within another string, returning true or false as appropriate.

Can take up to 2 parameters the second parameter is optional (by default is 0) and indicates de position where is going to start the searching.

.includes() method is case sensitive so be aware of that.

 let str = "Helloab World"; if ( str.includes("a") && str.includes("b") ){ console.log( "a and b received" ) } else { console.log("String do not containt a or b") } // Example with the second parameter. // Would go to the else statement because it would // start searching from the 8th position of the string console.log("Second if statement start here") if ( str.includes("a", 8) && str.includes("b", 8) ){ console.log( "a and b received" ) } else { console.log("String do not containt a or b") }

Hope this example can help you a little.

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