Transferred from: http://www.blogjava.net/pingpang/archive/2012/08/12/385342.html
1. JavaScript regular object substitution creation and usage:/pattern/flags A simple case study to understand what replace can do:
Regular expression constructors: New RegExp ("pattern" [, "flags"]);
Regular expression substitution variable function: stringobj.replace (regexp,replace Text);
Parameter description:
Pattern--a regular expression text
Flags--if present, will be the following values:
G: Global Match
I: Ignore case
GI: Above combination
//The following example is used to get the two parameters of the URL and return the real URL before urlrewritevarreg=NewREGEXP ("( http://www.qidian.com/BookReader/) (\\d+), (\\d+). aspx","GMI");varUrl="http://www.qidian.com/BookReader/1017141,20361055.aspx";//Way One, the simplest and most common wayvarRep=url.replace (Reg,"$1showbook.aspx?bookid=$2&chapterid=$3"); Console.log (rep); Results: http://www.qidian.com/BookReader/ShowBook.aspx?bookId=1017141&chapterId=20361055
Mode two, using a fixed-parameter callback function
var rep2=url.replace (reg,function (M,P1,P2,P3) {return p1+ "showbook.aspx?bookid=" +p3+ "&chapterid=" +p3});
alert (REP2);
Method Three, using the non-fixed parameter callback function
var rep3=url.replace (Reg,function () {var args=arguments; return args[1]+ "showbook.aspx?bookid=" +args[2]+ "& Chapterid= "+args[3];});
alert (REP3);
Method Four
Mode four and method three are very similar, but in addition to returning the replaced string, you can also get the parameters separately
var bookId;
var Chapterid;
function Captext ()
{
var args=arguments;
BOOKID=ARGS[2];
CHAPTERID=ARGS[3];
return args[1]+ "showbook.aspx?bookid=" +args[2]+ "&chapterid=" +args[3];
}
var rep4=url.replace (Reg,captext);
alert (REP4);
alert (BOOKID);
alert (Chapterid);
Use the test method to get the grouping
var reg3=new RegExp ("(http://www.qidian.com/BookReader/) (\\d+), (\\d+). aspx", "GMI");
Reg3.test ("http://www.qidian.com/BookReader/1017141,20361055.aspx");
Get three groups
alert (regexp.$1);
alert (regexp.$2);
alert (regexp.$3);
2. Learn the most commonly used test, exec, match, search, replace, split 6 methods
1) test checks whether the specified string exists
var data = "123123″; var recat =/123/ // true
Check if the character exists G continue down I case-insensitive
2) EXEC return query value
var data = "123123,213,12312,312,3, Cat,cat,dsfsdfs,"; var recat =/cat///cat
3) match gets query array
var data = "123123,213,12312,312,3, Cat,cat,dsfsdfs,"; var recat =/cat/gi; var arrmactches = data.match (recat) for (var i=0; i < Arrmactches.length; i++) { //cat Cat}
4) search returns location similar to IndexOf
var data = "123123,213,12312,312,3, Cat,cat,dsfsdfs,"; var recat =/cat///
5) Replace replacement character with regular substitution
var data = "123123,213,12312,312,3, Cat,cat,dsfsdfs,"; var recat =/cat/gi;alert (Data.replace (Recat, "libinqq"));
6) split using regular split array
var data = "123123,213,12312,312,3, Cat,cat,dsfsdfs,"; var recat =/\,/; var arrdata = data.split (recat); for (var0; i < arrdata.length; i++) { alert (arrdata[i]);}
3, Common expression collection:
"^\\d+$" //non-negative integers (positive integers + 0)"^[0-9]*[1-9][0-9]*$" //positive integers"^ ((-\\d+) | ( 0+)) $" //Non-positive integer (negative integer + 0)"^-[0-9]*[1-9][0-9]*$" //Negative integer"^-?\\d+$" //integer"^\\d+ (\\.\\d+)? $" //non-negative floating-point number (positive floating point + 0)"^ ([0-9]+\\.[ 0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\\. [0-9]+) | ([0-9]*[1-9][0-9]*)) $"//positive floating point number"^ ((-\\d+ (\\.\\d+)?) | (0+ (\\.0+)?)) $" //non-positive floating-point number (negative floating-point number + 0)"^ (-([0-9]+\\.[ 0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\\. [0-9]+) | ([0-9]*[1-9][0-9]*))) $"//Negative floating point number"^ (-?\\d+) (\\.\\d+)? $" //floating point number"^[a-za-z]+$" //A string consisting of 26 English letters"^[a-z]+$" //A string consisting of 26 uppercase letters in English"^[a-z]+$" //A string of 26 English letters in lowercase"^[a-za-z0-9]+$" //A string consisting of a number and 26 English letters"^\\w+$" //A string consisting of numbers, 26 letters, or underscores"^[\\w-]+ (\\.[ \\w-]+) *@[\\w-]+ (\\.[ \\w-]+) +$" //Email Address"^[a-za-z]+://(\\w+ (-\\w+) *) (\ \. ( \\w+ (-\\w+) *) * (\\?\\s*)? $" //URL"^[a-za-z0-9_]*$"。
============================================ the basic knowledge of regular expressions =======================================
^ matches the beginning of an input or a line,/^a/matches"An A", rather than matching"An a"$ matches the end of an input or a line,/a$/Matching"An a", rather than matching"An A"* matches the preceding metacharacters 0 or more times,/ba*/will match b,ba,baa,baaa+ Match Front metacharacters 1 or more times,/ba+/will match ba,baa,baaa? Matches the preceding metacharacters 0 or 1 times,/ba?/Match B,ba (x) match x Save X in named $1$9 variables in x|y matches x or y {n} exactly matches n times {n,} matches n more than {n,m} matches n-M-Times [XYZ] Character set (characterSet), matching any one by one characters (or metacharacters) in this collection [^XYZ] does not match any one of the characters in this set [\b] matches a backspace \b matches the boundary of a word \b matches a word's non-boundary \cx here, X is a control character,/\cm/Match Ctrl-M \d matches a character number character,/\d/=/[0-9]/\d matches a non-alphanumeric character,/\d/=/[^0-9]/\ n matches a newline character \ r matches a carriage return \s matches a white-space character, including a \n,\r,\f,\t,\v such as \s matches a non-whitespace, equals/[^\n\f\r\t\v]/\ t matches a tab \v match a straight tab \w match a character that can make up a word (alphanumeric, which is my paraphrase, with numbers), including underscores such as [\w] matches"$5.98"5 in, equal to [a-za-z0-9] \w matches a character that cannot be composed of words, such as [\w] matches"$5.98"In the $, equals [^a-za-z0-9]。
JS Regular expression/replace substitution variable method