javascript аналог preg_match_all
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | function preg_match_all(regex, haystack) { 'use strict' ; var globalRegex = new RegExp(regex, 'g' ), nonGlobalRegex = new RegExp(regex), nonGlobalMatch, globalMatch = haystack.match(globalRegex), matchArray, i; matchArray = []; for (i = 0; i < globalMatch.length; i += 1) { nonGlobalMatch = globalMatch[i].match(nonGlobalRegex); matchArray.push(nonGlobalMatch[1]); } return matchArray; } |
Комментарии
Получится:
function preg_match_all(regex, haystack) {
var globalRegex = new RegExp(regex, 'g');
var globalMatch = haystack.match(globalRegex);
matchArray = new Array();
nonGlobalRegex = new RegExp(regex);
for (var i in globalMatch) {
nonGlobalMatch = globalMatch.match(nonGlobalRegex);
matchArray.push(nonGlobalMatch[1]);
}
return matchArray;
}