// 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"]