HTML entity encoder and decoder

Make text safe to drop into HTML, or turn &, ' and friends back into readable characters.

Common entities

CharNamedNumericWhen to use
&&&Always — starts every entity
<&lt;&#60;Always in text
>&gt;&#62;Recommended
"&quot;&#34;Inside double-quoted attributes
'&#39;&#39;Inside single-quoted attributes (&apos; is HTML5 only)
nbsp&nbsp;&#160;Non-breaking space
©&copy;&#169;Optional with UTF-8
—&mdash;&#8212;Optional with UTF-8
€&euro;&#8364;Optional with UTF-8

Escaping in code

// JavaScript
const esc = (s) => s.replace(/[&<>"']/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[c]));

# Python
import html
html.escape('<b>"hi"</b>')     # &lt;b&gt;&quot;hi&quot;&lt;/b&gt;
html.unescape('&copy; 2026')   # © 2026

The JavaScript one-liner is itself a regex: a character class of the five special characters and a replacer function. Try variations in the regex tester. For URLs rather than markup, use the URL encoder.

Questions

Which characters must be escaped in HTML?

In text content, & and <. Inside attribute values, & and the quote character that delimits the attribute. Escaping > and both quote types everywhere is the simple, safe habit — that is what the "Minimal" mode does.

Does escaping HTML prevent XSS?

It prevents it in HTML text and quoted attributes, which is where most templates put data. It does not make data safe inside <script> blocks, inline event handlers, style attributes or javascript: URLs — those need context-specific encoding. Frameworks like React, Vue and Jinja escape text by default.

Do I need entities for accented letters or emoji?

Not if your page is served as UTF-8 (with <meta charset="utf-8">), which nearly every modern page is. Numeric entities are only useful when a file must stay pure ASCII.

What is the difference between named and numeric entities?

&copy; and &#169; (decimal) and &#xA9; (hex) all produce ©. Named entities are easier to read; numeric ones work for every Unicode character.