From 875633141a9344ceb69bbb494aec77129ef34abf Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Wed, 24 Feb 2010 11:03:47 -0500 Subject: [PATCH] merging rlfletchers branch with a safer _.template when the template delimiter includes RegExp characters. --- underscore.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/underscore.js b/underscore.js index f1f658fef..515897b62 100644 --- a/underscore.js +++ b/underscore.js @@ -30,6 +30,9 @@ // Export the Underscore object for CommonJS. if (typeof exports !== 'undefined') exports._ = _; + // Quick regexp-escaping function, because JS doesn't have RegExp.escape(). + var escapeRegExp = function(s) { return s.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1'); }; + // Create quick reference variables for speed access to core prototypes. var slice = Array.prototype.slice, unshift = Array.prototype.unshift, @@ -576,7 +579,7 @@ 'var p=[],print=function(){p.push.apply(p,arguments);};' + 'with(obj){p.push(\'' + str.replace(/[\r\t\n]/g, " ") - .replace(new RegExp("'(?=[^"+c.end[0]+"]*\\"+c.end.split('').join("\\")+")","g"),"\t") + .replace(new RegExp("'(?=[^"+c.end[0]+"]*"+escapeRegExp(c.end)+")","g"),"\t") .split("'").join("\\'") .split("\t").join("'") .replace(c.interpolate, "',$1,'")