JavaScript regular practice (will be updated continuously based on recent writing)
1. Javascript regular object replacement creation and usage: /pattern/flags First learn a simple case and understand what replace can do
Regular expression constructor: new RegExp("pattern"[,"flags"]);
Regular expression replacement variable function: stringObj.replace(RegExp, replace Text);
Parameter description:
pattern -- a regular expression text
flags -- If present, it will be the following value:
g: Global Match
i: Ignore case
gi: The above combination
//The following example is used to obtain two parameters of url and return the real Urlvar before urlRewrite reg=new RegExp("(http://www.qidian.com/BookReader/)(//d+),(//d+).aspx","gmi");var url="http://www.qidian.com/BookReader/1017141,20361055.aspx";//Method 1, the simplest and most commonly used method var rep=url.replace(reg,"$1ShowBook.aspx?bookId=$2&chapterId=$3");alert(rep);//Method 2, the callback function var with fixed parameters is used rep2=url.replace(reg,function(m,p1,p2,p3){return p1+"ShowBook.aspx?bookId="+p3+"&chapterId="+p3});alert(rep2);//Method 3, use the callback function with non-fixed parameters var rep3=url.replace(reg,function(){var args=arguments; return args[1]+"ShowBook.aspx?bookId="+args[2]+"&chapterId="+args[3];});alert(rep3);//Method 4//Method 4 is very similar to method 3. In addition to returning the replaced string, you can also get the parameter 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. The most commonly used test exec match search replace split 6 methods
1) test Check whether the specified string exists
var data = “123123″;
var reCat = /123/gi;
alert(reCat.test(data)); //true
//Check whether the character exists g Continue to go down i is case-sensitive
2) exec returns the query value
var data = "123123,213,12312,312,3,Cat,cat,dsfsdfs,";
var reCat = /cat/i;
alert(reCat.exec(data)); //Cat
3) match gets the 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++)
{
alert(arrMactches[i]); //Cat cat
}
4) search Return search position similar to indexof
var data = "123123,213,12312,312,3,Cat,cat,dsfsdfs,";
var reCat = /cat/gi;
alert(data.search(reCat)); //23
5) replace the replacement character using regular replacement
var data = "123123,213,12312,312,3,Cat,cat,dsfsdfs,";
var reCat = /cat/gi;
alert(data.replace(reCat,"libinqq"));
6) split using regular segmentation array
var data = "123123,213,12312,312,3,Cat,cat,dsfsdfs,";
var reCat = //,/;
var arrdata = data.split(reCat);
for (var i = 0; i < arrdata.length; i++)
{
alert(arrdata[i]);
}
3. Collection of commonly used expressions:
"^//d+$" //Non-negative integer (positive integer + 0)
"^[0-9]*[1-9][0-9]*$" //Positive integer
"^((-//d+)|(0+))$" //Not 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 number + 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 composed of 26 English letters
"^[AZ]+$" //A string composed of 26 English letters capitalization
"^[az]+$" //A string composed of lowercase of 26 English letters
"^[A-Za-z0-9]+$" //A string composed of numbers and 26 English letters
"^//w+$" //A string composed of numbers, 26 English letters or underscores
"^[//w-]+(//.[//w-]+)*@[//w-]+(//.[//w-]+)+$" //email address
"^[a-zA-z]+://(//w+(-//w+)*)(//.(//w+(-//w+)*))*(//?//S*)?$" //url
"^[A-Za-z0-9_]*$".
Basics of Regular Expressions
^ Matches an input or the beginning of a line, /^a/ matches "an A", but not "An a"
$ matches the end of an input or line, /a$/ matches "An a", but not "an A"
* Match the previous metacharacter 0 or more times, /ba*/ will match b,ba,baa,baa
+ Match the previous metacharacter 1 or more times, /ba+/ will match ba,baa,baaa
? Match the previous metacharacter 0 or 1 time, /ba?/ will match b,ba
(x) Match x saves x in a variable named $1...$9
x|y matches x or y
{n} exact match n times
{n,} Match more than n times
{n,m} Match nm times
[xyz] character set, matching any character (or metacharacter) in this set
[^xyz] does not match any character in this collection
[/b] Match a backspace character
/b Match the boundary of a word
/B Match a word's non-boundary
/cX Here, X is a control character, //cM/ matches Ctrl-M
/d matches a character, //d/ = /[0-9]/
/D matches a non-word numeric character, //D/ = /[^0-9]/
/n matches a newline character
/r matches a carriage return character
/s matches a whitespace character, including /n,/r,/f,/t,/v, etc.
/S matches a non-whitespace character equal to /[^/n/f/r/t/v]/
/t matches a tab character
/v matches a redirect tab character
/w matches a character that can form a word (alphanumeric, which is my translation, including numbers), including underscores, such as [/w] matches 5 in "$5.98", equals [a-zA-Z0-9]
/W Match a character that cannot form a word, such as [/W] matches $ in "$5.98", which is equal to [^a-zA-Z0-9].