var selector = "div.class > span:not(:first-child) a[href]" var chunker = /((?:\([^\)]+\)|\^\+\]|[^ ,\(\[]+)+)(\s*,\s*)?/g; var parts = []; // Reset the position of the chunker regexp (start from beginning) chunker.lastIndex = 0; // Collect the pieces while ((m = chunker.exec(selector)) !== null) { parts.push(m[1]); // Stop if we've countered a comma if (m[2]) { extra = RegExp.rightContext; break; } }
其中,正则中的 \^\+\] 会如何匹配?
最后的结果很奇怪…
问题补充:
运行语句
m = chunker.exec(selector)
得到的匹配中,第三个为:span:not(:first-child)。
但看你提供的解释,并不符合要求。既不是一个()里面的,也不是不含有空格、逗号、左小括号、左中括号。
我认为的结果,应该分成2个,span:not和(:first-child)。前面和最后一个匹配,括号则和第一个匹配。那为什么正确结果却是span:not(:first-child)?
问题补充:
m = chunker.exec(selector)
应该是m = selector.match(chunker)
这样可获得所有匹配。我不理解第三个匹配的结果,再帮我分析下,万分感谢!