Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@alexanderGugel
Created November 12, 2014 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexanderGugel/c77c65ac881da9e82f41 to your computer and use it in GitHub Desktop.
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
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