Use of capture groups in String.split()

javascript, regex

Solution

In your second result, `a` is appearing because you've wrapped it in a capture group `()` (parentheses).

If you want to not include it but you still require a conditional group, use a non-capturing group: `(?:a)`. The questionmark-colon can be used inside any capture group and it will be omitted from the resulting list of captures.

Here's a simple example of this in action: http://regex101.com/r/yM1vM4

Problem

``` $ node > "ababaabab".split(/a{2}/) [ 'abab', 'bab' ] > "ababaabab".split(/(a){2}/) [ 'abab', 'a', 'bab' ] > ``` So, this doesn't make sense to me. Can someone explain it? I don't get why the `'a'` shows up. Note: I am trying to match for doubled line endings (possibly on windows files) so I am splitting on `/(\r?\n){2}/`. However I get extraneous `'\015\n'` entries in my array (note `\015 == \r`). Why are these showing up? Note: also affects JS engine in browsers so this is specific to JS not node.

Original source

Related problems