I know almost all browsers and Node installations support it, but Node 11 doesn't, and I kinda need that right now. So enjoy!
// Useable just like the normal matchAll:
let data = "Hello World!";
let rx = /[A-Z]/g;
let matches = [...data.matchAll(rx)];
console.table(matches);
/* Expected output:
┌─────────┬─────┬───────┬────────────────┬───────────┐
│ (index) │ 0 │ index │ input │ groups │
├─────────┼─────┼───────┼────────────────┼───────────┤
│ 0 │ 'H' │ 0 │ 'Hello World!' │ undefined │
│ 1 │ 'W' │ 6 │ 'Hello World!' │ undefined │
└─────────┴─────┴───────┴────────────────┴───────────┘
*/
I think there's an edge case this doesn't handle:
See https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/js/es6/string/matchall.js for a more complete polyfill. It doesn't handle unicode, but you can fix that by changing one the advancement based on whether the regex has
unicode
flag or not (see the link in the comment).More generally, Closure library is usually a great source for simple yet battle-tested polyfills. (Definitely much better than the