Home >Web Front-end >JS Tutorial >JavaScript method to determine whether a string contains a specified substring_javascript skills
The example in this article describes how JavaScript determines whether a string contains a specified substring. Share it with everyone for your reference. The specific analysis is as follows:
The following JS code defines a contains method for the String object to determine whether the string contains substrings, which is very useful.
if (!Array.prototype.indexOf) { Array.prototype.indexOf = function(obj, start) { for (var i = (start || 0), j = this.length; i < j; i++) { if (this[i] === obj) { return i; } } return -1; } } if (!String.prototype.contains) { String.prototype.contains = function (arg) { return !!~this.indexOf(arg); }; }
The following is a detailed usage example, which can be executed in the browser
I hope this article will be helpful to everyone’s JavaScript programming design.