Home > Web Front-end > JS Tutorial > body text

js method to determine whether a string contains a substring_javascript skills

WBOY
Release: 2016-05-16 16:17:55
Original
1269 people have browsed it

The example in this article describes how js determines whether a string contains a substring. Share it with everyone for your reference. The details are as follows:

In our daily front-end development, we often encounter the problem of determining whether a string contains a certain substring. Here we will explore some methods to solve this requirement and use them correctly. Ideally, what we're looking for is a method that matches our purpose (if x contains y) and returns true or false.

1. String.prototype.indexOf and String.prototype.lastIndexOf

These two methods may be the easiest for us to think of. If it contains a substring, it will return an index greater than or equal to 0, otherwise it will return -1, which does not meet our ideal situation.

Copy code The code is as follows:
var str = "My blog name is Benjamin-Focus on front-end development and user experience",
​ substr = "Benjamin";

function isContains(str, substr) {
Return str.indexOf(substr) >= 0;
}

//true
console.log(isContains(str, substr));

2. String.prototype.search

We thought of the String.prototype.search method. Since the parameter of the search method is a regular expression, it is the same as indexOf.

Copy code The code is as follows:
var str = "My blog name is Benjamin-Focus on front-end development and user experience",
​ substr = "Benjamin";

function isContains(str, substr) {
Return new RegExp(substr).test(str);
}

//true
console.log(isContains(str, substr));

This method looks better than the indexOf method. This method directly returns true or false. At the same time, the method name test is more semantic than indexOf.

3. String.prototype.contains

Copy code The code is as follows:
var str = "My blog name is Benjamin-Focus on front-end development and users Experience",
​ substr = "Benjamin";

function isContains(str, substr) {
Return str.contains(substr);
}

//true
console.log(isContains(str, substr));

This method is currently only supported by Firefox and is still in the ECMAScript 6 draft. This method satisfies the ideal situation mentioned above. Please click here for details. If you want to use the contains method, you can refer to the third-party library string.js. Click here to download string.js from this site. Source code implementation:
Copy code The code is as follows:
contains: function(ss) {
Return this.s.indexOf(ss) >= 0;
},
Other methods are to be added. . .
Of course, regarding performance issues, which method is faster remains to be tested. Interested friends may wish to test it themselves.

I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template