I've tried &&, ||, and some other parison 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 mand 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)
I've tried &&, ||, and some other parison 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 mand 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)
Share Improve this question asked Dec 27, 2020 at 17:55 Piipperi800Piipperi800 951 silver badge8 bronze badges 2-
Can you provide some examples of how you've tried
&&
,||
and other parison methods? – Turtlean Commented Dec 27, 2020 at 18:07 -
You can use regex to match the cases with string, in your case, the regex will be
/(a*b)/g
– Kaustubh Commented Dec 27, 2020 at 18:08
6 Answers
Reset to default 3if(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('word1') && str.includes('word2')){
console.log("word1 and word2 received");
}
if(str.includes("a") && str.includes("b")) {
console.log("a and b received")
}
You can also read from the docs: https://developer.mozilla/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.
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.