How can I check that a string does not include the text of another string?

var include_flag = toState.name.includes("home.subjects.subject.exams.exam.tests");
return !include_flag;

Using the JS includes method would probably be your best bet. I get I'm a little late in answering this question, but I was just googling this myself and came up with this answer after some fiddling with the code. This code will return true if toState.name does NOT include the string given.

Hope this helps anyone searching the same question I had!


You could use includes and negate it.

!str1.includes(str2)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes


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

Tags:

Javascript