[Js] - determines whether the string contains a string

String object methods

Method One: indexOf () (recommended)

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

the indexOf () method returns a string value of the specified position of the first occurrence of the string. If the string value to be retrieved is not present, then the method returns -1.

 

Method two: search () 

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

search () method for substring search character string specified, retrieving or regular expression matching substring. If no matches any substring, or -1.

 

Method three: match ()

var str = "123";
var reg = RegExp(/3/);
if(str.match(reg)){
    // 包含        
}

match () method retrieves the value specified in string, or to find one or more regular expression matching.

 

RegExp object methods

Method four: test () 

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

test () method retrieves the value specified in the string. Returns true or false.

 

Method five: exec ()

var str = "123";
var reg = RegExp(/3/);
if(reg.exec(str)){
    // 包含        
}

exec () method is used to match the regular expression string retrieval. It returns an array in which to store the matching results. If a match is not found, the return value is null.

 

https://www.cnblogs.com/ooo0/p/7741651.html

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

the indexOf () method returns a string value of the specified position of the first occurrence of the string. If the string value to be retrieved is not present, then the method returns -1.

 

Method two: search () 

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

search () method for substring search character string specified, retrieving or regular expression matching substring. If no matches any substring, or -1.

 

Method three: match ()

var str = "123";
var reg = RegExp(/3/);
if(str.match(reg)){
    // 包含        
}

match () method retrieves the value specified in string, or to find one or more regular expression matching.

 

RegExp object methods

Method four: test () 

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

test () method retrieves the value specified in the string. Returns true or false.

 

Method five: exec ()

var str = "123";
var reg = RegExp(/3/);
if(reg.exec(str)){
    // 包含        
}

exec () method is used to match the regular expression string retrieval. It returns an array in which to store the matching results. If a match is not found, the return value is null.

 

https://www.cnblogs.com/ooo0/p/7741651.html

Guess you like

Origin www.cnblogs.com/zjt-blogs/p/11583863.html