Simplify backreference adjustment

This commit is contained in:
c-spencer 2011-05-19 13:07:42 +01:00
commit 6b660009ad

View file

@ -53,12 +53,9 @@ var Tokenizer = function(rules) {
// Count number of matching groups. 2 extra groups from the full match
// And the catch-all on the end (used to force a match);
var matchcount = new RegExp("(?:(" + state[i].regex + ")|(.))").exec("a").length - 2;
// Get how long references can be, depending on how many capturing groups have been found so far.
var reflength = ("" + (matchTotal + matchcount.length - 2)).length;
// Replace any backreferences and offset appropriately.
var adjustedregex = state[i].regex.replace(new RegExp("\\\\([0-9]{1," + reflength + "})", "g"), function (match, digit) {
var adjustedregex = state[i].regex.replace(/\\([0-9]+)/g, function (match, digit) {
return "\\" + (parseInt(digit, 10) + matchTotal + 1);
});