Global Mode


var exp    = new ApgExp(pattern, "g");

The pattern match attempt is made at the index, exp.lastIndex. The user can set exp.lastIndex to any value prior to the match attempt. If a match is not found exp.lastIndex is incremented by one and the attempt repeated. This repeats until either a match is found or the end of string is reached. In global mode if a match is found, exp.lastIndex is incremented by the length of the matched pattern, or one if the length is zero. (Some patterns allow matches to empty strings. In this case exp.lastIndex is incremented by one to prevent infinite loops and to allow the global search to continue.) This allows for an iteration over all matched patterns in the input string. When the match fails, exp.lastIndex is then reset to zero.


var pattern, str, exp, result;    
pattern = 'pattern = "abc"\n';
str = "---abc---ABC---";
exp = new ApgExp(pattern, "g");
while(result = exp.exec(str)){
  console.log("found: " + result[0] + " :at: " + result.index);
/* returns */
found: abc :at: 3
found: ABC :at: 9