Digadogup
Digadogup

Reputation: 41

Javascript match issue, non-exhaustive?

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

Answers (1)

Nina Scholz
Nina Scholz

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

Related Questions