Reputation: 26908
In my Chrome console, I tried this out:
> "abcd".match(/(.+)+/)
< ["abcd", "abcd"]
the fact that match
returned two 'results' was, to me, unexpected and odd. I tested in Firefox and the result was the same (so I presume it's not a bug of any sort).
However, in Python:
> re.findall(r'(.+)+', 'abba')
< ['abba']
I can't explain this. What's going on?
Upvotes: 0
Views: 113
Reputation: 5384
I think in javascript, .match gives group 0 (entire matched expression w/o capture) as well as group 1 (captured group) whereas python's findAll behaves differently-- showing you only captured groups, and not the whole matched portion (group 0)
Upvotes: 2
Reputation: 46657
According to MDN:
If the regular expression does not include the g flag, returns the same result as regexp.exec(string).
And from exec:
The returned array has the matched text as the first item, and then one item for each capturing parenthesis that matched containing the text that was captured.
So the first item in the returned array is the match ("abcd"
) and the second item is the one and only parenthesized group (happens to also be "abcd"
).
Upvotes: 2