Skip to content

Instantly share code, notes, and snippets.

@ValorLin
Created April 19, 2018 02:14
Show Gist options
  • Save ValorLin/6a612c26ae0223e7d9d701bc8978a1db to your computer and use it in GitHub Desktop.
Save ValorLin/6a612c26ae0223e7d9d701bc8978a1db to your computer and use it in GitHub Desktop.
matchAll for RegExp
function matchAll(str, re) {
let result = [];
let match = null;
while ((match = re.exec(str)) !== null) {
result.push(match);
}
return result;
}
// Example
// matchAll('1.a 2.b 3.c', /\d\.([a-z])/g)
// returns [ [ "1.a", "a" ], [ "2.b", "b" ], [ "3.c", "c" ] ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment