Chunker Regular Expression Filter Analysis in jQuery source code _ jquery-js tutorial

Source: Internet
Author: User
This is the longest Regular Expression in Jq. I have been studying it for a long time and have been confused. I still feel that it is easier to understand the analysis value step by step through debugging. The Code is as follows:

Var chunker = /((? :\((? : \ ([^ ()] + \) | [^ ()] +) + \) | \[(? : \ [[^ [\] * \] | ['"] [^'"] * ['"] | [^ [\]'"] +) + \] | \\. | [^> + ~, (\ [\] +) + | [> + ~]) (\ S *, \ s *)? ((? :. | \ R | \ n) *)/g,


This is the longest Regular Expression in Jq. I have been studying it for a long time and have been confused. I still feel that it is easier to understand the analysis value step by step through debugging,

I tried to make the image more intuitive and distinguished it by different colors, such:


Group 1 splits an array one by one using the following code:

The Code is as follows:


// Here, the loop is used to split each selector into a parts array. For example, p # id> p ul li is split into ['P # id', '>', 'P ', 'ul ', 'lil']
While ((chunker.exe c (""), m = chunker.exe c (soFar ))! = Null ){
// SoFar stores the separator string after the first layer, that is, group 3 on the image.
SoFar = m [3];
// The first part of the selector block is pushed into the array.
Parts. push (m [1]);
// If it is split into commas (,), the previous Group ends, jumps out of the loop, and goes to another group of selectors.
If (m [2]) {
// Record another set of selectors
Extra = m [3];
Break;
}
}


Others, such as ID and class, are easy to understand.

The Code is as follows:


Match :{
// \ U00c0-\ uFFFF matches letters of multiple countries or families
ID :/#((? : [\ W \ u00c0-\ uFFFF-] | \.) +)/, // For example: # myId
CLASS :/\.((? : [\ W \ u00c0-\ uFFFF-] | \.) +)/, // such as. myClass
NAME:/\ [name = ['"] * (? : [\ W \ u00c0-\ uFFFF-] | \.) +) ['"] * \]/, // For example: [name =" myName "]
ATTR:/\ [\ s *((? : [\ W \ u00c0-\ uFFFF-] | \.) +) \ s *(? :( \ S? =) \ S * (['"] *) (. *?) \ 3 |) \ s * \]/, // For example, [attribute = "value"]
TAG:/^ ((? : [\ W \ u00c0-\ uFFFF \ *-] | \.) +)/, // such as: p
CHILD:/:( only | nth | last | first)-child (? : \ (Even | odd | [\ dn +-] *) \)? //, // For example: first-child or: nth-child (5n + 1)
POS:/:( nth | eq | gt | lt | first | last | even | odd )(? : \ (\ D *)\))? (? = [^-] | $)/, // For example: nth (3) span
PSEUDO :/:((? : [\ W \ u00c0-\ uFFFF-] | \.) + )(? : \ (['"]?) ((? : \ ([^ \)] + \) | [^ \ (\)] *) +) \ 2 \))? ///: Jlkj \ kjl ('kl (kklk) kl ')
}


To be continued ......

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.