js determines whether a string contains another string

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

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

2, match (): retrieve a value specified in string, or to find one or more regular expression matching.

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

3, search (): for the specified search character string substring, or retrieve the regular expression substring match. If no matches any substring, or -1.

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

4, () test: the value for the specified search character string. Returns true or false.

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

5, exec (): being used in the search character string expression matching. It returns an array in which to store the matching results. If a match is not found, the return value is null.

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

Guess you like

Origin www.cnblogs.com/shirliey/p/11764410.html