Last Match Properties
After a successful pattern-matching attempt, the apgExp object retains information about the last-matched patterns or phrases. Unlike the result object which retains all matched phrases for all rule names, the apgExp object retains only the last matched.
Syntax
var exp = new apgExp(pattern[, flags]); var result = exe.exec(input);
Last Match Properties
- exe.input (alias exe["$_"]): A copy of the input string.
- exe.lastMatch (alias exe["$&"]): A copy of the last-matched pattern - same as result[0]
- exe.leftContext (alias exe["$`"]): Prefix of the input string up to the first character of the matched pattern.
- exe.rightContext (alias exe["$'"]): Suffix of the input string, the phrase following the matched pattern.
- exe.rules (alias exe["${rule-name}"]): The last-matched phrase for the named rule.
If Unicode mode is true, i.e. flags = "u", all strings are arrays of integer character codes, otherwise it is a JavaScript string.
See Also
Example
var pattern, exp, str, result;
pattern = 'word = alpha *(alpha / num)\n';
pattern += 'alpha = %d65-90 / %d97-122\n';
pattern += 'num = %d48-57\n';
exp = new apgExp(pattern);
str = "---ab12...";
result = exp.exec(str);
console.log("exp.leftContext : "+exp.leftContext);
console.log('exp["$`"] : '+exp["$`"]);
console.log("exp.lastMatch : "+exp.lastMatch);
console.log('exp["$&"] : '+exp["$&"]);
console.log("exp.rightContext: "+exp.rightContext);
console.log('exp["$\'"] : '+exp["$'"]);
console.log("exp.rules.word : "+exp.rules.word);
console.log("exp[${word}] : "+exp["${word}"]);
console.log("exp.rules.alpha : "+exp.rules.alpha);
console.log("exp[${alpha}] : "+exp["${alpha}"]);
console.log("exp.rules.num : "+exp.rules.num);
console.log("exp[${num}] : "+exp["${num}"]);
/* result */
exp.leftContext : ---
exp["$`"] : ---
exp.lastMatch : ab12
exp["$&"] : ab12
exp.rightContext: ...
exp["$'"] : ...
exp.rules.word : ab12
exp[${word}] : ab12
exp.rules.alpha : b
exp[${alpha}] : b
exp.rules.num : 2
exp[${num}] : 2