Last active
June 17, 2020 06:54
-
-
Save hasanuzzamanbe/563d853feb24c0b245d01b66ff81f40f to your computer and use it in GitHub Desktop.
Polyfill for matchAll. example:matchAll use casse; yourString.matchAll( /your-regex/ )
This file contains 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
// polyfill for matchAll | |
function matchAllPolyfill(regexPattern, sourceString) { | |
let output = [] | |
let match | |
// make sure the pattern has the global flag | |
let regexPatternWithGlobal = RegExp(regexPattern, "g") | |
while (match = regexPatternWithGlobal.exec(sourceString)) { | |
delete match.input | |
output.push(match) | |
} | |
return output | |
} | |
// You can call to get array output | |
// let matches = matchAllPolyfill(regEx, yourString); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For an old browser like Microsoft edge, there is no advanced function support like .matchAll
You can use this polyfill function to support those browsers.
just call like this
matchAllPolyfill(regEx, yourString);