Created
October 4, 2010 17:02
-
-
Save simonw/610054 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
/* Inspired by Python's useful re.findall method */ | |
function findall(regex, str) { | |
var matches = [], m; | |
regex.lastIndex = 0; | |
while (m = regex.exec(str, regex.lastIndex)) { | |
matches[matches.length] = m; | |
} | |
return matches; | |
} | |
/* Example usage: | |
var matches = findall(/\d+/, '123 456 789'); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When I ran this in node and in Firefox I got stuck in an infinite loop beucase regex.lastIndex is always 0 - I think your function requires regex to be global so the example regex should be /\d+/g?
I think you can get the same result with '123 456 789'.match(/\d+/g)?