1. Method of String object

IndexOf () method

var str = "123"; console.log(str.indexOf("3") ! = 1); // trueCopy the code

Pass in a string, return -1 if it doesn’t match, matching the first occurrence of the return string.

Search () method

var str = "123"; console.log(str.search("3") ! = 1); // trueCopy the code

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

Match () method

var str = "123"; var reg = RegExp(/3/); If (str.match(reg)){// include}Copy the code

The match() method retrieves a specified value within a string or finds a match for one or more regular expressions.

2.RegExp object methods

The test () method

var str = "123";
var reg = RegExp(/3/);
console.log(reg.test(str)); // true
Copy the code

The test() method is used to retrieve the value specified in the string. Returns true or false. The exec () method

var str = "123"; var reg = RegExp(/3/); If (reg.exec(STR)){// include}Copy the code

The exec() method is used to retrieve a match for a regular expression in a string. Returns an array containing the matching results. If no match is found, the return value is null.