Escape HTML entities

Escape or unescape HTML entities (replace characters like <,>, &, " and \' with their HTML version)

loading Escape HTML entities
About this tool

What are HTML entities and when do you escape them?

An HTML entity is a piece of text that starts with & and ends with ; and stands for one character: &lt; is <, &amp; is &, &copy; is the copyright sign. Entities exist because a few characters have a job in HTML. A < opens a tag and an & opens an entity, so if you want the browser to show them as plain text, you have to write them as entities. That step is called escaping, and the reverse is unescaping.

Escaping matters in three everyday situations. The first is showing code or user input on a page: a comment that contains <script> must reach the browser as &lt;script&gt;, otherwise it is read as markup (this is the core of defending against cross-site scripting when you build HTML by hand). The second is putting text inside an attribute, such as title="..." or content="...", where a stray " ends the value early. The third is working with data that already arrives escaped: RSS feeds, JSON from a CMS, scraped pages and email source are full of &amp; and &#39; that you need to turn back into readable text.

This page does both directions in two separate cards, and everything runs in your browser; the text is never sent anywhere. Escaping handles the five characters that can break HTML (& < > " '). Unescaping understands every entity your browser knows: named ones such as &eacute; and &hellip;, decimal ones such as &#169; and hexadecimal ones such as &#xA9;. The list of named entities is defined by the HTML Living Standard.

How to escape and unescape HTML entities

  1. In the first card, type or paste the text in the Your string field. The escaped version appears right below, as you type.
  2. Decide what to do with accents and symbols. By default only & < > " ' are changed, which is enough when your page is served as UTF-8. Turn on the switch for non-ASCII characters if the text has to survive a channel that is not UTF-8 safe: every character above U+007F, such as é, 日 or an emoji, is then written as a numeric hexadecimal entity.
  3. Press Copy escaped to put the result on the clipboard.
  4. To go the other way, use the second card: paste text full of entities in the escaped-string field. The readable text appears in the field below it.
  5. Press Copy unescaped to copy it.

Both cards open with an example already filled in, so you can see the behaviour before pasting your own text. The decoding is done through a text-only element of the browser, so nothing in your input is ever executed or loaded, even if it looks like a script or an image tag.

Examples

Escaping a snippet that has quotes and an ampersand

Input:

<a href="/x?a=1&b=2">Tom & Jerry's</a>

Escaped:

&lt;a href=&quot;/x?a=1&amp;b=2&quot;&gt;Tom &amp; Jerry&#39;s&lt;/a&gt;

Note that the & inside the URL also became &amp;. That is correct inside HTML: a browser turns it back into & when it reads the attribute.

Accents and symbols as numeric entities

With the non-ASCII switch on, Café São Paulo — preço: R$ 5 < 10 becomes:

Caf&#xE9; S&#xE3;o Paulo &#x2014; pre&#xE7;o: R$ 5 &lt; 10

Every character above U+007F is written as &#x plus its code point in hexadecimal. With the switch off, the accents stay as they are and only < is changed.

Reading a feed or a scraped page

Input:

&lt;p&gt;Ol&aacute;, mundo&hellip;&lt;/p&gt;

Unescaped:

<p>Olá, mundo…</p>

Named entities, decimal ones (&#169;) and hexadecimal ones (&#xA9;) all give the same ©.

Text that was escaped twice

&amp;lt; unescapes once to &lt;, not to <. That is the usual sign of double escaping: a value that was escaped, stored, and escaped again. Run the unescape twice to reach <, then fix the code that escapes twice.

Common problems and how to fix them

  • The page shows &amp; or &lt; as literal text: the text was escaped twice. Unescape it once to confirm, then remove the second escaping step in your template or in the code that saves the value.
  • An accented letter shows as é or ?: this is a character set problem, not an entity problem. Serve the page as UTF-8 with <meta charset="utf-8">, or turn on the non-ASCII switch to make the text independent of the encoding.
  • &nbsp; seems to disappear after unescaping: it becomes a non-breaking space (U+00A0), which looks like a normal space but is a different character. Search for it when a comparison with a plain space fails.
  • An entity without the final semicolon is not converted: some browsers accept a few legacy names such as &copy without ;, but you should always write the semicolon. &#169; and &#xA9; need it too.
  • Escaping did not stop an injection in JavaScript or in a URL: HTML escaping protects text placed in HTML content and quoted attributes. For a value inside a script, use JSON encoding; inside a URL, use URL encoding.

Frequently asked questions

What is the difference between &amp; and &#38;?

None for the browser: both are the ampersand. &amp; is the named entity and &#38; the decimal numeric one (&#x26; is the hexadecimal one). Named entities are easier to read, numeric ones work for any character, including those with no name.

Which characters must always be escaped in HTML?

< and & in text, and the quote that delimits the value in an attribute (" or '). Escaping > is not strictly required but is customary, and this page does all five so the result is safe in both contexts.

Is HTML escaping the same as URL encoding or Base64?

No. HTML escaping makes text safe inside a document, URL encoding makes it safe inside an address (%20 for a space), and Base64 turns bytes into plain letters and digits. Using one where another is needed is a common source of bugs.

How do I type a character that has no key on my keyboard?

Use its numeric entity: &#xA9; for the copyright sign, &#x20AC; for the euro. To find a code point from a character, try text to Unicode.

Doing it in code

JavaScript

const escapeHtml = s => s
  .replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
  .replace(/"/g, '&quot;').replace(/'/g, '&#39;');

// Decoding in a browser: a textarea never runs what it holds.
const area = document.createElement('textarea');
area.innerHTML = '&lt;p&gt;Ol&aacute;&lt;/p&gt;';
console.log(area.value); // <p>Olá</p>

Python

import html

print(html.escape('<a href="/x?a=1&b=2">Tom & Jerry\'s</a>'))
# &lt;a href=&quot;/x?a=1&amp;b=2&quot;&gt;Tom &amp; Jerry&#x27;s&lt;/a&gt;
print(html.unescape('&lt;p&gt;Ol&aacute;&hellip;&lt;/p&gt; &#169;'))
# <p>Olá…</p> ©

Go

package main

import (
	"fmt"
	"html"
)

func main() {
	fmt.Println(html.EscapeString(`<a href="/x?a=1&b=2">Tom & Jerry's</a>`))
	fmt.Println(html.UnescapeString("&lt;p&gt;Ol&aacute;&hellip;&lt;/p&gt; &#169;"))
}

The libraries differ only in how they write the quotes (&quot; or &#34;, &#39; or &#x27;); all of them are read back identically by any browser.

Report a problem