javascript - NodeJS RegExp: how to access full list of results -
i'm having trouble accessing full list of results when .exec()
regular expression in node. here code:
var p = /(aaa)/g; var t = "someaaa textaaa toaaa testaaa aaagainst"; p.exec(t); > [ 'aaa', 'aaa', index: 4, input: 'someaaa textaaa toaaa testaaa aaagainst' ]
i 2 results, no matter what. error on regexp itself?
any appreciated!
exec
return first matched result. results
use
match
var p = /(aaa)/g; var t = "someaaa textaaa toaaa testaaa aaagainst"; var matches = t.match(p);
var p = /(aaa)/g, t = "someaaa textaaa toaaa testaaa aaagainst"; var matches = t.match(p); console.log(matches);
use
while
exec
while(match = p.exec(t)) console.log(match);
var p = /(aaa)/g, t = "someaaa textaaa toaaa testaaa aaagainst"; var matches = []; while (match = p.exec(t)) { matches.push(match[0]); } console.log(matches);
Comments
Post a Comment