check whether a string contains a substring in JavaScript?
var string = "foo",
substring = "oo";
string.includes(substring)
includes doesn’t have IE support, though. In an ES5 or older environment, String.prototype.indexOf, which returns −1 when it doesn’t find the substring, can be used instead:
var string = "foo",
substring = "oo";
string.indexOf(substring) !== -1
No comments:
Post a Comment