headerphoto

Sticky Mode

Syntax

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 the attempt fails and returns null. In sticky 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 sticky search to continue.) This allows for an iteration over all consecutive matched patterns in the input string. When the match fails, exp.lastIndex is then reset to zero.

Example 1

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

Example 2

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

Example 3

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