Previous Article: Evaluate () method. If a given string (with only one parameter) matches this pattern, true is returned. Otherwise, false is returned. In the regular expression,/I,/g,/ig,/gi,/m ..
Previous: http://www.2cto.com/kf/201112/115493.html
JavaScript Regular -- RegExp:
RegExp has a test () method. If a given string (with only one parameter) matches this mode, it returns true. Otherwise, it returns false.
Differences and meanings of/I,/g,/ig,/gi,/m in Regular Expressions
/I (case-insensitive)
/G (full-text search for all matching characters)
/M (multi-row search)
/Gi (full-text search, case-insensitive)
/Ig (full-text search, case-insensitive)
Var str = "jfkd @ s, laj @ fd, safd @ saf, ds @";
Var res = // @/gi; // full-text search @
Alert (res. test (str); // output: true
The exec () method has a string parameter and returns an array. The first entry of the array is the first match, and the rest is the reverse reference.
// AStr only contains the first instance, which is @ in "jfkd @ sla @
Var aStr = res.exe c (str );
Alert (aStr );
The String object has a match () method, which returns an array containing all matching strings.
Alert (str. match (res); // output :@,@,@,@
The behavior of another string method called seartch () is similar to that of indexOf (), but it uses a RegExp object rather than just a substring. The Search () method returns a matched position in the string.
// It starts from the position of the Character index [0]
Alert (str. search (res); // output: 4
Replace ():
/**
* Replace (), which can replace all the matches of a substring (the first parameter) with another string (the second parameter)
*/
Var sRep = str. replace ("@", "slim horse ");
Alert (sRep); // output: jfkd slim horse s, laj @ fd, safd @ saf, ds @
/**
* You can also use a regular expression as the first parameter.
*/
Alert (str. replace (res, "slim horse"); // output: jfkd slim horse s, laj slim horse fd, safd slim horse saf, ds slim horse
/**
* You can also specify a function as the second parameter of replace.
*/
Var sFun = str. replace (res, function (){
Return "Skinny horse ";
});
Alert ("---------------" + sFun); // output: jfkd, laj, fd, safd, safd, and ds
/**
* Split ()
*/
Alert (str. split ("@"));
// Use a regular expression to implement the same function
Alert (str. split (/@/));
Onkeyup = "this. value = this. value. replace (/\ D/g ,'')"
Onafterpaste = "this. value = this. value. replace (/\ D/g,'') "/>
All metacharacters in the expression must be escaped (followed by a backslash) to match correctly. Metacharacters are part of the regular expression syntax. Below are all metacharacters used by the regular expression: ([{\ ^ $ | )? * +. Therefore, if you want to match a question mark, then: var reTest = /\? /; Or, var reTest = new RegExp ("\\? "); The second line uses two backslash, called double escape.
Character class: