What is HTML escaping?
In HTML, < starts a tag and & starts a character reference. To display these characters literally — or to put user input into a page safely — you replace them with character references (often called HTML entities). This is HTML escaping. The browser shows the original character, but the parser no longer treats it as markup.
| Character | Named entity | Numeric reference |
|---|---|---|
& | & | & |
< | < | < |
> | > | > |
" | " | " |
' | ' | ' |
| © | © | © / © |
For example, <a href="x">Tom & Jerry's</a> escapes to <a href="x">Tom & Jerry's</a>. With the non-ASCII option, café 😀 becomes café 😀.
When to use it
- Showing HTML or XML source code in a blog post or documentation
- Fixing symbols that break in email templates or a CMS
- Checking that user input is correctly escaped as part of XSS (cross-site scripting) testing
- Turning scraped text full of
&and"back into readable text
Escaping in code
| Language | Escape | Unescape |
|---|---|---|
| Python | html.escape(s) | html.unescape(s) |
| PHP | htmlspecialchars($s, ENT_QUOTES) | html_entity_decode($s) |
| Java | StringEscapeUtils.escapeHtml4(s) (Apache Commons Text) | StringEscapeUtils.unescapeHtml4(s) |
| Go | html.EscapeString(s) | html.UnescapeString(s) |
| Ruby | CGI.escapeHTML(s) | CGI.unescapeHTML(s) |
| C# / .NET | WebUtility.HtmlEncode(s) | WebUtility.HtmlDecode(s) |
| JavaScript (lodash) | _.escape(s) | _.unescape(s) |
| JavaScript (DOM) | set element.textContent instead of innerHTML | new DOMParser().parseFromString(s, 'text/html').documentElement.textContent |
Most template engines (React JSX, Jinja2, Blade, Thymeleaf, Go's html/template) escape output automatically, so manual escaping is usually needed only when you build HTML strings yourself.
An escapeHtml function for JavaScript
Plain JavaScript has no built-in function to escape a string for HTML. This small function covers the five characters that matter:
function escapeHtml(str) {
return String(str).replace(/[&<>"']/g, (c) => ({
'&': '&', '<': '<', '>': '>', '"': '"', "'": ''',
}[c]));
}
escapeHtml(`<a href="x">Tom & Jerry's</a>`);
// → '<a href="x">Tom & Jerry's</a>'
Replacing all characters in a single pass avoids a classic bug: if you chain several .replace() calls, & must be replaced first, otherwise the & in < gets escaped again.
Which characters to escape, and where
| Where the text goes | Must escape | Notes |
|---|---|---|
Between tags: <p>…</p> | & < | Escaping > too is harmless and common |
Double-quoted attribute: title="…" | & " | Always quote attribute values |
Single-quoted attribute: title='…' | & ' | Escaping all five characters covers both styles |
URL attribute: href="…" | HTML-escape and check the scheme | Reject javascript: URLs; URL-encode query values |
Inside <script> or <style> | HTML escaping does not apply | Use JSON encoding (e.g. JSON.stringify) and avoid </script> |
Common HTML entities
| Entity | Character | Code point |
|---|---|---|
| non-breaking space | U+00A0 |
© / ® / ™ | © / ® / ™ | U+00A9 / U+00AE / U+2122 |
— / – | — / – | U+2014 / U+2013 |
… | … | U+2026 |
€ | € | U+20AC |
← / → | ← / → | U+2190 / U+2192 |
× / · | × / · | U+00D7 / U+00B7 |
In a UTF-8 page you can type these characters directly; entities are only needed for the five special characters above, or when a system cannot handle non-ASCII text.
What unescape supports
Unescaping handles every named entity the browser knows ( , ©, … …), decimal references such as © and hexadecimal references such as ©. It is decoded once, so &amp; becomes & — use “Use result as input” to decode double-escaped text again.
Frequently asked questions
Why is the single quote escaped as ' instead of '?
' is not defined in HTML 4, so some older browsers and email clients do not recognize it. The numeric reference ' works everywhere.
Is HTML escaping enough to prevent XSS?
Escaping is the core defense for text inside HTML elements and quoted attribute values. Other contexts need different handling: javascript: URLs in href, data written inside <script> or event handler attributes, and CSS.
Which entities can be unescaped?
All named entities such as and ©, decimal references such as ©, and hexadecimal references such as ©.
Is my input sent anywhere?
No. The conversion runs entirely in your browser; nothing you type is uploaded or stored.