By the end of this tutorial, you’ll understand Lodash’s escape RegExp, how it differs from safe interpolate tags, and when raw HTML output is appropriate.
01
RegExp, not fn
escape matches delimiter tags—like interpolate and evaluate.
02
Raw output tags
Default open-percent-minus tags skip HTML escaping.
03
Safe interpolate
Default value tags call _.escape automatically.
04
XSS awareness
Never pass user input through raw-output tags.
05
Trusted HTML
Use escape tags for vetted markup partials only.
06
Per-template override
Pass a custom escape RegExp in options.
Fundamentals
What Is _.templateSettings.escape?
_.templateSettings.escape is a RegExp that marks delimiter tags for unescaped value insertion in Lodash templates. Despite the name, it does not assign an escape function—it defines which syntax produces raw HTML output (default: open-percent-minus tags).
💡
Beginner tip — naming is confusing
In Lodash, interpolate tags (open-percent-equals) are the safe, HTML-escaped path. escape tags (open-percent-minus) output raw strings. For user comments or names, always use interpolate—not escape.
Customize the escape RegExp when you need a different raw-output delimiter while keeping interpolate on another syntax (for example Mustache braces for values and ERB-minus for trusted partials).
Foundation
📝 Syntax
Assign a RegExp to the global settings object (or pass it per compile):
The property name is historical ERB terminology—read Lodash docs, not the label alone.
Use _.escape() in JavaScript when pre-processing strings outside templates.
Never compile templates from untrusted users regardless of tag type.
Wrap Up
Conclusion
_.templateSettings.escape configures which delimiter inserts unescaped values into Lodash templates. For everyday dynamic text, use interpolate tags. Reserve escape-pattern tags for trusted HTML snippets you control.
Next, learn logic blocks with evaluate, then inject helpers via imports.
Use interpolate tags for all user-facing dynamic text
Reserve escape tags for vetted HTML from your CMS or static partials
Document which fields are allowed in raw-output tags
Run security reviews when templates render external data
Use _.escape() when building strings outside _.template()
❌ Don’t
Assign a function to templateSettings.escape
Pipe user comments or names through raw-output tags
Assume the name “escape” means automatic HTML safety
Mix up templateSettings.escape and the _.escape() utility
Compile template source supplied by end users
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about escape
Use these points when choosing Lodash output tags.
5
Core concepts
📝01
RegExp
Raw delimiter pattern.
Basics
🔒02
Interpolate
Safe escaped path.
Security
⚠️03
Raw tags
Trusted HTML only.
Risk
🔧04
_.escape()
Separate utility fn.
Tool
⚙️05
evaluate
Logic comes next.
Next step
❓ Frequently Asked Questions
It is a RegExp that tells _.template() which tags insert values without HTML escaping. The default pattern matches open-percent-minus tags (the ERB-style raw-output delimiter).
A RegExp, like interpolate and evaluate. Lodash uses it at compile time to find raw-output tags. Do not assign a callback function to templateSettings.escape.
Standard interpolate tags (open-percent-equals) call _.escape on output. Escape-pattern tags (open-percent-minus) output raw HTML and are unsafe for user input.
Only for trusted HTML you control—pre-sanitized markup, static partials, or server-generated snippets. Never pipe user text through raw-output tags.
interpolate tags escape HTML entities before printing. escape tags (despite the name) skip escaping and insert the value as-is into the result string.
At compile time. Set templateSettings.escape before calling _.template(). Recompile after changing the RegExp.
Did you know?
Lodash inherited ERB’s three-tag model from Ruby templates. The minus sign in the raw-output delimiter marks “do not HTML-escape this insert”—opposite of what beginners often guess from the setting name escape.