merging rlfletchers branch with a safer _.template when the template delimiter includes RegExp characters.

This commit is contained in:
Jeremy Ashkenas
2010-02-24 11:03:47 -05:00
parent 3482ec1259
commit 875633141a

View File

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