Created
April 19, 2018 02:14
-
-
Save ValorLin/6a612c26ae0223e7d9d701bc8978a1db to your computer and use it in GitHub Desktop.
matchAll for RegExp
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 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