Why do javascript sub-matches stop working when the g modifier is set?
var text = 'test test test test';
var result = text.match(/t(e)(s)t/);
// result: ["test", "e", "s"]
the above works fine... [1] is e and [2] is s... perfect
var result = text.match(/t(e)(s)t/g);
// ["test", "test", "test", "test"]
the above it screwed... it ignores my sub-matches
var result = text.match(/test/g);
for (i in result) {
console.log(result[i].match(/t(e)(s)t/));
}
/*
["test", "e", "s"]
["test", "e", "s"]
["test", "e", "s"]
["test", "e", "s"]
*/
is the above the only valid solution?
thanks to htw the proper way to do this would be the following:
var pattern = new RegExp(/t(e)(s)t/g);
while (match = pattern.exec(text))
{
console.log(match);
}