javascript - Why does this non-capturing group capture? -
applying regex pattern:
/(?:(^| |\>|\+))+([a-z\-\_]+)/gi
to string:
body.test ol+li ol > li #foobar p>span a[href=*]
i these matches, comma separated:
body, ol,+li, ol, > li, p,>span,
why matches have leading space ,
>
, +
sign? i'd expect part of regex (?:(^| |\>|\+))
match signs, not capture them.
edit: trying match html tags , css selectors contributing css specificity of css selector. want match each li
or span
or forth on own, without +
or >
.
capturing not same matching. since you're specifying combinators in pattern, picked matcher, regardless of whether they're captured or non-captured.
to capture, need exec()
regular look on string , loop through results, will contain capture groups. i've cleaned pattern , modified doesn't capture unnecessarily , recognize the general sibling combinator ~
:
var sel = "body.test ol+li ol > li #foobar p>span a[href=*]"; var re = /(?:^| |>|\+|~)+([a-z_-]+)/gi; var matches = [], m; while (m = re.exec(sel)) { matches.push(m[1]); }
you obtain expected matches:
body, ol, li, ol, li, p, span,
javascript regex css-selectors pattern-matching
No comments:
Post a Comment