functionGetCookie (name)//take a cookie function { //Coook in document.cookie = "age=12; Name=1.css "; vararr = Document.cookie.match (NewRegExp ("(^|)" + name + "= ([^;] *)(;|$)")); if(Arr! =NULL)returnUnescape (arr[2]);return NULL; } //GetCookie (name); //arr[0]= "Name=1.css" //arr[1]= "" //arr[2]= "1.css" //arr[3]= "" //Why does arr return an array of length 4? //in the blog Park and Csdn asked a lot of not an accurate answer, and then in a step by step experiment found the answer//first step: First match "(^|)" + name + "= ([^;] *)(;|$)" //The second step: in the match () inside is also (^|), ([^;] *),(;|$) //The answer is "(^|)" + name + "= ([^;] *)(;|$)",(^| ),([^;] *), (; |$). Also returns an array of arr length 4 //Note: It is important to match the first step to match the second step. No person returns null directly //from this extrapolate, we use JavaScript to get the parameters functionRequest (name) {//url=http://localhost:9120/cookie.html?mly=1&c=12 vararr = Window.location.search.match (NewREGEXP ("([(^) (&) (?)])" + name + "= ([^&]*) (&|$)")); if(Arr! =NULL)returnUnescape (arr[2]);return NULL; } //Request ("mly");//return 1 //Note: In the previous we said () there will be multiple matches, and continuing in () will continue to match //For example: "N". Match (New RegExp ("((1)) 2") returns an array of length 3, arr[0]=12,arr[1]=1,arr[2]=1; //For example: "a". Match (New RegExp ("(1) | ( 2)) 2 ") will return an array of length 4, arr[0]=12,arr[1]=1,arr[2]=1,arr[3]=undefined; //arr[3]=undefined on the right (2) is not used for the first match, and then returns to undefined when matched with (2); This also proves how many () will return () + 1-length arrays //where in ([(^) (&) (?)]) (), but because in [], there is no need to follow the above rules
Usage of match and regexp combinations in JavaScript