gramm gramm - 4 years ago 137
Javascript Question

How to check if one string contains another substring in Javascript?

Usually, I would expect a

method, but there doesn't seem to be one. What is a reasonable way to check for this?

Answer Source

String.prototype.indexOf returns the position of the string in the other string. If not found, it will return -1:

var string = "foo",
    substring = "oo";
console.log(string.indexOf(substring) !== -1);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download