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,'")