問題は解決しましたが、別の問題に遭遇しました。修正のフィドルは次のとおりです。http://jsfiddle.net/Axvgf/
変更されたメソッドは次のとおりです。
function colorize(text, pos) {
var i = 0, current_times = 0;
var startc = '(', endc = ')';
var current = -1;
var entities = {'>': '>','<':'<'};
var p2 = 0;
var regex = new RegExp(Object.keys(entities).join("|"),'g');
var converted = text.replace(regex, function(x, j) {
if(pos > j) p2 += entities[x].length - 1;
return entities[x];
});
pos += p2;
var parens = [], indices = [], o = {};
var newText = converted.replace(/((?:\\)*)([()])/g, function(full, escape, x, idx) {
var len = escape.split(/\\/g).length - 1;
if (len % 2 == 0) {
indices.push(idx);
if (x == startc) ++i;
o[idx] = { selected: false, type: x, depth: i, idx: idx, pair: -1, extra: escape };
if (idx == pos) o[idx].selected = true;
if (x == startc) parens.push(idx);
else {
if (parens.length > 0) {
var p = parens.pop();
o[idx].pair = p;
if (o[p].selected) o[idx].selected = true;
o[p].pair = idx;
if (o[idx].selected) o[p].selected = true;
}
--i
}
}
});
newtext = converted;
indices = indices.sort(function(x,y) { return Number(y) - Number(x); });
indices.forEach(function(i) {
newtext = newtext.substr(0,i) + o[i].extra +
"<span class='" + (o[i].pair == -1 ? "unmatched " : "paren_" + (o[i].depth % 5)) +
(o[i].selected ? " selected_paren": "") + "'>" + o[i].type + "</span>" +
newtext.substr(i + 1 + o[i].extra.length)
});
return newtext;
}