HTML Escape: Escape and Unescape HTML Text

Escape text for HTML content or a double-quoted attribute, or turn named and numeric character references back into characters. This is not an HTML-to-text converter.

Action
Escape destination

One unescape pass. < stays < rather than becoming a less-than sign.

Character preview

Shown with textContent, so markup is not parsed as HTML.

Nothing to preview.

Paste source


  

Replacements

  • & becomes &. Required in text and in double-quoted attributes.
  • < becomes &lt;. Required in text; also escaped in attributes.
  • > becomes &gt;. Escaped in both so the two destinations stay aligned.
  • " becomes &quot;. Required in a double-quoted attribute; also escaped in text.
  • ' becomes &#39;. Escaped in both. The numeric form works even where &apos; is not recognized.

How it works

The named set is the semicolon-terminated table from the HTML Standard.

Escape replaces only &, <, >, ", and '. Emoji and other characters stay as themselves. The destination changes the paste example and which of those five the HTML syntax requires. It does not change the replacements. An apostrophe becomes &#39;. Characters that already look like references are escaped again, so the input spelling &amp; becomes &amp;amp;.

Unescape reads semicolon-terminated references in one left-to-right pass. Names must match the HTML table in the same case: &lt; and &LT; are both less-than, while &Lt; is a different character. Decimal forms are &# plus digits. Hexadecimal forms are &#x or &#X plus hex digits. One pass turns &amp;lt; into &lt;. Escaping that result produces &amp;lt; again. A second unescape is a separate action and is what produces <.

An ambiguous ampersand is &, then ASCII letters or digits, then ;, when that whole name is not in the table. &notit; is ambiguous because not is a real name and a legacy parser may consume the shorter one, leaving the rest behind. &notin; is the real name and decodes. A real name missing its semicolon, such as &amp before a space, is invalid. Each problem lists the character position of the ampersand, counting Unicode code points from 1, plus the line and column. The reference is left unchanged. AT&T is ordinary text.

A numeric reference may not be NULL, a carriage return, a surrogate, a noncharacter, or a control other than tab, line feed, and form feed. Values above U+10FFFF are invalid. This tool does not remap &#128; through &#159; with Windows-1252, and it does not insert U+FFFD. Browsers do both while parsing.

The character preview is assigned with textContent, so a decoded tag stays visible as characters. Copy uses the clipboard API and, if that fails, a textarea fallback. Download saves a text file on this device. Nothing is uploaded or stored. The result is not a JavaScript, CSS, or URL escape, and it is not for an unquoted attribute, an event handler, or a script or style element.