Monday, 22 April 2019

check whether a string contains a substring in JavaScript?


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

remove a property from a JavaScript object?

remove a property from a JavaScript object? delete myObject.regex; // or, delete myObject['regex']; // or, var prop = ...