Escape HTML for Safe Text
textContent instead. This narrow encoder is not a sanitizer and does not make values safe for URLs, event handlers, style/CSS, unquoted attributes, or other output contexts.Arguments
text: string —Raw user text to escape for HTML.
Returns
string — Solution idea
This encoder converts five characters into HTML entities so a raw value is treated as text when interpolated into the text-content portion of a constructed HTML string. Prefer assigning Element.textContent when writing text into a live DOM node because it avoids parsing markup at all. Output encoding is context-specific: this function is not URL validation, a markup sanitizer, or safe handling for event-handler, style/CSS, unquoted-attribute, or arbitrary attribute contexts.
escapeHtml('<div class="x">Tom & Jerry's</div>')
// => "<div class="x">Tom & Jerry's</div>"
escapeHtml('5 > 3 & 2 < 4')
// => "5 > 3 & 2 < 4"Solution
Overview
This encoder converts five characters into HTML entities so a raw value is treated as text when interpolated into the text-content portion of a constructed HTML string. Prefer assigning Element.textContent when writing text into a live DOM node because it avoids parsing markup at all. Output encoding is context-specific: this function is not URL validation, a markup sanitizer, or safe handling for event-handler, style/CSS, unquoted-attribute, or arbitrary attribute contexts.
Approach 1: Regex + lookup map (preferred)
Create a map for the five characters and replace them with a single regex pass. Use the result only as HTML text content inside a constructed markup string; for a live DOM node, prefer textContent. Complexity is O(n) time and O(n) space.
export default function escapeHtml(text) {
const map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return String(text).replace(/[&<>"']/g, (ch) => map[ch]);
}
export default function escapeHtml(text: string): string {
const map: Record<string, string> = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return String(text).replace(/[&<>"']/g, (ch) => map[ch]);
}
Approach 2: Chained replace (simple but order-sensitive)
You can chain replace calls, but you must escape & first or you will double-escape the entities you create later.
export default function escapeHtml(text) {
return String(text)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
export default function escapeHtml(text: string): string {
return String(text)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
Notes & Pitfalls
Escaping is context-specific. This HTML-text encoder does not make strings safe for URLs, event handlers, JavaScript, style/CSS, unquoted attributes, or arbitrary attribute contexts. If you chain replacements, escape &first or you will double-escape entities.Escaping does not allow rich HTML; use a well-maintained sanitizer with an allowlist when you must accept markup. For live DOM text, assign textContentinstead of constructing an HTML string.
Empty string should return an empty string. Already-escaped input will be escaped again (expected for raw-text encoding). Quotes are encoded too, but that does not broaden this exercise beyond the documented HTML text-content context.
Regex replacement with a lookup table. String normalization via String(text). Context-aware output encoding.
Common mistakes on this challenge
escapeHtml replacement map or order is incorrect
Escaping should map all HTML special chars, including &before entity-like sequences.Convert input to string and replace ampersand, angle brackets, double quote, and apostrophe. Ensure ampersands are escaped before other substitutions, even in existing entity-like text. Leave text without escapable chars unchanged. Use this encoder only for the documented HTML text-content context; live DOM text should use textContent.
Resources
Similar questions
Guides
Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.
Built in public
The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.