Created
December 30, 2019 21:55
-
-
Save bathos/2d1c9f7ab3c1250ee33bac4213f4a1c4 to your computer and use it in GitHub Desktop.
observing-last-index-values.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function matchAllButUpdateOriginalRegExp(regExp, string) { | |
return regExp[Symbol.matchAll].call({ | |
constructor: { [Symbol.species]: function() { return regExp; } }, | |
flags: regExp.flags, | |
lastIndex: regExp.lastIndex | |
}, string); | |
} | |
const pattern = /./g; | |
console.group('Ordinary behavior'); | |
for (let match of 'abc'.matchAll(pattern)) { | |
console.log(match, pattern.lastIndex); | |
} | |
console.groupEnd(); | |
console.group('Subversive shenanigans'); | |
for (let match of matchAllButUpdateOriginalRegExp(pattern, 'abc')) { | |
console.log(match, pattern.lastIndex); | |
} | |
console.groupEnd(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment