js to determine whether a string contains a string of five methods

Method 1: indexOf() (recommended)

 var str = "123"; console.log(str.indexOf("3") != -1 ); // true

The indexOf() method returns the first occurrence of a specified string value in a string. If the string value to be retrieved does not appear, the method returns -1.

Method 2: search()

 var str = "123"; console.log(str.search("3") != -1 ); // true

The search() method is used to retrieve a specified substring in a string, or to retrieve a substring that matches a regular expression. Returns -1 if no matching substring is found.

Method 3: match()

 var str = "123"; var reg = RegExp(/3/);

The post js Five methods for determining if a string contains a certain string first appeared on Lenix Blog .

This article is reprinted from https://blog.p2hp.com/archives/9661
This site is for inclusion only, and the copyright belongs to the original author.