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
RegExp.prototype.execAll = function(string) { | |
var match = null; | |
var matches = new Array(); | |
while(match = this.exec(string)) { | |
var matchArray = []; | |
for(i in match) { | |
if(parseInt(i) == i) { | |
matchArray.push(match[i]); | |
} | |
} | |
matches.push(matchArray); | |
} | |
return matches; | |
} | |
// Example | |
var someTxt = 'abc123 def456 ghi890'; | |
var results = /[a-z]+(\d+)/g.execAll(someTxt); | |
// Output | |
// [ | |
// ["abc123", "123"], | |
// ["def456", "456"], | |
// ["ghi890", "890"] | |
// ] |
How can I match multiple occurrences with a regex in JavaScript similar to PHP's preg_match_all()? - Stack Overflow