Why does "abcabcabc".match(/(abc){3}/);
return two entries ["abcabcabc", "abc"]
? both "abcabcabc" and "abc"
similarly,
["1234", "1234"]
Every regexp has an implicit capturing group around it, which is stored in result 0.
The second result comes from your capturing group (abc)
. Most regexp implementations only keep the last match of that capturing group.
1st Capturing Group
(abc){3}
{3}
Quantifier — Matches exactly 3 timesA repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data.
abc
matches the charactersabc
literally (case sensitive)
So the first value is the whole match, ie "abcabcabc"
.
The second value is the last iteration captured by (abc)
, ie "abc"
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.