Usage of match and regexp combinations in JavaScript

Source: Internet
Author: User

 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.