It's not very concise, but this:
var matches = [];
'1234567'.replace
(
/(?=[0-9]{2})/gi,
function(s,pos,str)
{
matches.push(str.substr(pos, 2));
}
);
will set matches
to ['12','23','34','45','56','67']
. It works by using a lookahead assertion to find matches without actually swallowing them. (The replace
doesn't actually replace anything in this case; it's just a convenient way to apply a closure to all instances of a regex-match in a given string.)