Check if a string contains a substring in JavaScript
Since ES6 you can now use the built in includes
method eg string.includes(substring)
.
For example
"testing".includes("test");
// true
One thing to note is this is case sensitive so the below will be false.
"Testing".includes("test");
// false
There is also an optional position param which defaults to 0.
For example the below will return true.
"sand".includes("and");
"sand".includes("and", 1);
However this will return false, because we are only including the part of the string after charecter 2 eg nd
"sand".includes("and", 2);
For more information be sure to read about includes on the MDN website.