Created
November 12, 2014 00:15
-
-
Save alexanderGugel/c77c65ac881da9e82f41 to your computer and use it in GitHub Desktop.
JS: Return all matches instead of only the first one when executing a regular expression on a string
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
var execMultiple = function (regex, str) { | |
var match = regex.exec(str); | |
var matches = []; | |
while (match != null) { | |
matches.push(match); | |
match = regex.exec(str); | |
} | |
return matches; | |
}; | |
// Example | |
console.log(execMultiple(/(?:\(\?\:)([^)]+)/g, "(?:browser)((.+?))(?:,)")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment