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

  1. 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);

  1. 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);

read: match vs exec in javascript


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -