Reputation: 41
why does console.log("1010101".match(/101/g));
equal 2 not 3?
when there are 3 occurrences of "101"
Why is this the case?
Upvotes: 1
Views: 102
Reputation: 386550
That is because matched patterns are not matched anymore. You could use a positive lookahead and match only the first 1
and lookahead of the rest of the wanted pattern.
console.log("1010101".match(/1(?=01)/g).length);
Upvotes: 1