Skip to content

Instantly share code, notes, and snippets.

@hasanuzzamanbe
Last active June 17, 2020 06:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hasanuzzamanbe/563d853feb24c0b245d01b66ff81f40f to your computer and use it in GitHub Desktop.
Save hasanuzzamanbe/563d853feb24c0b245d01b66ff81f40f to your computer and use it in GitHub Desktop.
Polyfill for matchAll. example:matchAll use casse; yourString.matchAll( /your-regex/ )
// 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);
@hasanuzzamanbe
Copy link
Author

hasanuzzamanbe commented Jun 17, 2020

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment