Blog.csdn.net/weixin_4298…
String object
- indexof()
- IndexOf () returns the first occurrence of a specified string value in a string.
- If the string value to retrieve is not present, the method returns -1.
var str = "The People's Republic of China";
console.info(str.indexOf("The people")! = -1); // true
console.info(str.indexOf("Chinaman")! = -1); // false
Copy the code
- search()
- Search () is used to retrieve a substring specified in a string, or to retrieve a substring that matches a regular expression.
- If no matching substring is found, -1 is returned.
var str = "The People's Republic of China";
console.info(str.search("The people")! = -1); // true
console.info(str.search("Chinaman")! = -1); // false
Copy the code
- match()
- The match() method retrieves a specified value within a string or finds a match for one or more regular expressions.
- Returns: an array of matching results. The contents of the array depend on whether regexp has the global flag G. Returns null if no match is found.
var str="The rain in SPAIN stays mainly in the plain";
if(str.match(/ain/gi)) {/ / contains
}
Copy the code
The RegExp object
- test()
- The test() method is used to retrieve the value specified in the string. Returns true or false.
var str = "123";
var reg = RegExp(/ 3 /);
console.log(reg.test(str)); // true
Copy the code
- exec()
- 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.
var str = "123";
var reg = RegExp(/ 3 /);
if(reg.exec(str)){
/ / contains
}
Copy the code