$ 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.
Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d", "o", and "g".
Numbers for Named Capturing Groups. Mixing named and numbered capturing groups is not recommended because flavors are inconsistent in how the groups are numbered. If a group doesn't need to have a name, make it non-capturing using the (?:group) syntax.
Definition and Usage The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With