Evolution of selector cutting Regular Expressions

Source: Internet
Author: User
  • // Tags, Id pseudo-class attribute identifiers, their main part, parent-child adjacent brother parallel wildcard, blank var Reg =/[\ W \ u00a1-\ Uffff] [\ W \ u00a1-\ Uffff-] * | [#.: \ [] [\ W \ (\) \] + | \ s * [> + ~, *] \ S * | \ s +/g
  • // Disable the contents in the parentheses from VAR Reg =/[\ W \ u00a1-\ Uffff] [\ W \ u00a1-\ Uffff-] * | [#.: \ [] (? : [\ W \ u00a1-\ Uffff-] | \ ([^ \)] * \) | \]) + | (? : \ S *) [> + ~, *] (? : \ S *) | \ s +/g
  • // Make sure that the attribute selector is used as a complete word var Reg =/[\ W \ u00a1-\ Uffff] [\ W \ u00a1-\ Uffff-] * | [#. :] (? : [\ W \ u00a1-\ Uffff-] | \ s * \ ([^ \)] * \) + | \ [[^ \] * \] | (? : \ S *) [> + ~, *] (? : \ S *) | \ s +/g
  • // Narrow down the range of the descendant selector var Reg =/[\ W \ u00a1-\ Uffff] [\ W \ u00a1-\ Uffff-] * | [#.:] (? : [\ W \ u00a1-\ Uffff-] | \ s + \ ([^ \)] * \) + | \ [[^ \] * \] | (? : \ S *) [> + ~, *] (? : \ S *) | \ s (? = [\ W \ u00a1-\ Uffff * #. [:])/g
  • Fix useless blank strings on the left

    VaR reg_split =/^ \ s + | [\ W \ u00a1-\ Uffff] [\ W \ u00a1-\ Uffff-] * | [#.:] (? : [\ W \ u00a1-\ Uffff-] | \ s + \ ([^ \)] * \) + | \ [[^ \] * \] | (? : \ S *) [> + ~, *] (? : \ S *) | \ s + (? = [\ W \ u00a1-\ Uffff *#. [:])/g; var selectors = "[AAA = 99]> dfdsf, ggg eee iii "; // var selectors = "[AAA = EEE] jjjjj" // var selectors = "P span" Var array = [], I = 0, rI = 0 selectors. replace (reg_split, function (selector) {If (selector = false) {// if it is a blank string I ++ & (array [ri ++] = ""); // and it is not the first captured} else if (selector. match (/^ \ s * ([> + ~, *]) \ S * $/) {array [ri ++] = Regexp. $1 ;}else {array [ri ++] = selector ;}}); alert ("|" + array. join ("| "));
  • Corrected the matching Regular Expression between useless blank characters and pseudo-class selector

 var reg_split =/^ \ s + | [\ W \ u00a1-\ Uffff] [\ W \ u00a1-\ Uffff-] * | [#. :] [\ W \ u00a1-\ Uffff-] + (?: \ ([^ \)] * \)? | \ [[^ \] * \] | (?: \ S *) [> + ~, *] (?: \ S *) | \ s (? = [\ W \ u00a1-\ Uffff *#. [:])/g; var selectors = "Div: eq (0)"; var parts = [], I = 0, rI = 0 selectors. replace (reg_split, function (Part) {I ++ if (Part = false) {// if it is a blank string if (I) parts [ri ++] = ""; // and it is not the first captured} else if (part. match (/^ \ s * ([> + ~, *]) \ S * $/) {parts [ri ++] = Regexp. $1 ;}else {parts [ri ++] = part ;}}); 
VaR reg_split =/^ [\ W \ u00a1-\ Uffff \-\ *] + | [#.:] [\ W \ u00a1-\ Uffff-] + (?: \ ([^ \)] * \)? | \ [[^ \] * \] | (?: \ S *) [> + ~,] (?: \ S *) | \ s (? = [\ W \ u00a1-\ Uffff * #. [:]) | ^ \ s +/; var slim =/\ s + | \ s * [> + ~, *] \ S * $/function spliter (expr) {var flag_break = false; var full = []; // place the word used to cut a single selector group to ", "Var parts = []; // place the word used to cut a single selector group and use the link selector as the do {expr = expr. replace (reg_split, function (Part) {If (Part = ",") {// This cutter only processes the first parallel selector flag_break = true ;} else {If (part. match (SLIM) {// link, parallel, and blank spaces on both sides of the operator selector are processed // parts is reversed because Div. aaa, which is processed first after reverse. AAA full = full. concat (parts. reverse (), part. replace (/\ s/g ,' '); Parts = [];} else {parts [parts. length] = part;} return ""; // remove the processed part}); If (flag_break) break;} while (expr) Full = full. concat (parts. reverse ());! Full [0] & full. shift (); // remove the first blank at the beginning of return full;} var expr = "div> Div # AAA, span" console. log (spliter (expr); // ["Div", ">", "# AAA", "Div"]

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.