-
-
Save albingeorge/43aceaf0d1601de52011 to your computer and use it in GitHub Desktop.
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
// Using native JavaScript... | |
function getMatches(str, regex) { | |
var matches = []; | |
var match; | |
if (regex.global) { | |
regex.lastIndex = 0; | |
} else { | |
regex = new RegExp(regex.source, 'g' + | |
(regex.ignoreCase ? 'i' : '') + | |
(regex.multiline ? 'm' : '') + | |
(regex.sticky ? 'y' : '')); | |
} | |
while (match = regex.exec(str)) { | |
// If you want to use regex.lastIndex in this loop, you'd need more | |
// code here to fix IE < 9 | |
matches.push(match); | |
if (regex.lastIndex === match.index) { | |
regex.lastIndex++; | |
} | |
} | |
return matches; | |
} |
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
// Using XRegExp... | |
function getMatches(str, regex) { | |
return XRegExp.forEach(str, regex, function(match) { | |
this.push(match); | |
}, []); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment