ES6 version of this is (check out answer from Allison):
!str1.includes(str2)
The original accepted answer was:
You are looking for indexOf
var x = "home.subjects.subject.exams.exam.tests";
console.log(x.indexOf('subjects')); // Prints 5
console.log(x.indexOf('state')); // Prints -1
Answer from Ananth on Stack OverflowWhy includes() works with a string?
How to check if a string contains text from an array of substrings in JavaScript? - Stack Overflow
How to use includes method to see if string is an of array of specific strings?
Formula for Countifs, with string includes text1 or text2
Videos
ES6 version of this is (check out answer from Allison):
!str1.includes(str2)
The original accepted answer was:
You are looking for indexOf
var x = "home.subjects.subject.exams.exam.tests";
console.log(x.indexOf('subjects')); // Prints 5
console.log(x.indexOf('state')); // Prints -1
You could use includes and negate it.
!str1.includes(str2)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
There's nothing built-in that will do that for you, you'll have to write a function for it, although it can be just a callback to the some array method.
Two approaches for you:
- Array
somemethod - Regular expression
Array some
The array some method (added in ES5) makes this quite straightforward:
if (substrings.some(function(v) { return str.indexOf(v) >= 0; })) {
// There's at least one
}
Even better with an arrow function and the newish includes method (both ES2015+):
if (substrings.some(v => str.includes(v))) {
// There's at least one
}
Live Example:
const substrings = ["one", "two", "three"];
let str;
// Setup
console.log(`Substrings: ${substrings}`);
// Try it where we expect a match
str = "this has one";
if (substrings.some(v => str.includes(v))) {
console.log(`Match using "${str}"`);
} else {
console.log(`No match using "${str}"`);
}
// Try it where we DON'T expect a match
str = "this doesn't have any";
if (substrings.some(v => str.includes(v))) {
console.log(`Match using "${str}"`);
} else {
console.log(`No match using "${str}"`);
}
Regular expression
If you know the strings don't contain any of the characters that are special in regular expressions, then you can cheat a bit, like this:
if (new RegExp(substrings.join("|")).test(string)) {
// At least one match
}
...which creates a regular expression that's a series of alternations for the substrings you're looking for (e.g., one|two) and tests to see if there are matches for any of them, but if any of the substrings contains any characters that are special in regexes (*, [, etc.), you'd have to escape them first and you're better off just doing the boring loop instead. For info about escaping them, see this question's answers.
Live Example:
const substrings = ["one", "two", "three"];
let str;
// Setup
console.log(`Substrings: ${substrings}`);
// Try it where we expect a match
str = "this has one";
if (new RegExp(substrings.join("|")).test(str)) {
console.log(`Match using "${str}"`);
} else {
console.log(`No match using "${str}"`);
}
// Try it where we DON'T expect a match
str = "this doesn't have any";
if (new RegExp(substrings.join("|")).test(str)) {
console.log(`Match using "${str}"`);
} else {
console.log(`No match using "${str}"`);
}
One line solution
substringsArray.some(substring=>yourBigString.includes(substring))
Returns true/false if substring exists/doesn't exist
Needs ES6 support