Skip to tool
ecech.
💻 Developer & Code

Escape the Ampersand First, or You Escape Your Own Escapes

Replace < before & and &lt; becomes &amp;lt;. Only five characters matter, and the order you replace them in is one of them.

Result



Characters changed

Size change

Double-encoded

&amp;lt; and friends

Non-ASCII

What needed escaping, and where

Advertisement

How the calculation works

Encoding <b> by hand, two orders < first <b> &lt;b&gt; &amp;lt;b&amp;gt; shows as &lt;b&gt; & first <b> <b> &lt;b&gt; shows as <b> The ampersand you introduce gets escaped by the next step, so it goes first. Decoding runs the other way: entities resolve last, not first.

How to Use This Tool

Paste text to make it safe to drop into HTML, or paste HTML to see the plain characters back. The counters underneath show whether the input was already encoded — the usual reason a page displays &amp; to a reader.

Only five characters matter

Despite the size of the entity tables floating around, the working list is short:

  • &&amp; — always, because it starts every entity.
  • <&lt; — because it starts a tag.
  • >&gt; — not strictly required in text, and cheap insurance.
  • "&quot; — needed inside a double-quoted attribute.
  • '&#39; — needed inside a single-quoted attribute.

Escape all five everywhere and you never have to think about which context you are in. It costs a few bytes and removes an entire category of bug.

The ampersand goes first

Encoding by hand fails in one predictable way. Replace < with &lt; first, then replace & with &amp;, and the second pass mangles what the first produced: &lt; becomes &amp;lt;, and the page displays &lt; to the reader instead of a less-than sign.

The ampersand must be replaced before anything that introduces one. Decoding runs the opposite way — resolve &amp; last, or you turn &amp;lt; into a working tag, which is a real security bug rather than a cosmetic one.

Context decides the minimum text content < & 2 characters quoted attribute < & and that quote 3 unquoted attribute space tab > = " ' ` Just quote your attributes. The unquoted case is a foot-gun with no upside.
Escaping all five characters everywhere avoids having to track which context you are in.

Entities are not for accented letters

Writing &eacute; for é or &#20013; for 中 is a habit from the days of single-byte encodings. Modern pages are UTF-8, and accented letters, CJK characters and emoji are all perfectly legal typed directly.

Encoding them anyway makes the file bigger, the source unreadable, and the text harder to search — &eacute; does not match a search for é. The "everything" option exists because you will occasionally meet a legacy system that needs it, not because it is a good default.

Escaping is not the same as sanitising

Escaping turns text into text: after it, <script> displays as characters and does nothing. That is the right answer when the content should never be markup.

If the content is meant to contain markup — a rich-text editor, user-submitted HTML — escaping is the wrong tool, because it would show the tags rather than apply them. That needs a sanitiser with an allowlist of permitted elements and attributes, which is a much harder problem and not one to solve with string replacement.

Advertisement

Frequently Asked Questions

Which characters need to be escaped in HTML?
Five in practice: & < > " and '. Only & and < are strictly required in text content, and a quote is required inside an attribute using that quote. Escaping all five everywhere costs a few bytes and removes the need to track context.
Why does my page show & instead of &?
The text was encoded twice. Something escaped the ampersand, then escaped the result again, turning & into &amp;. Find the layer applying the second pass rather than decoding twice to compensate — a double decode breaks the moment the input arrives correctly encoded.
What order should I escape HTML characters in?
The ampersand first, always. Replace < before & and the < you just created becomes &lt;, because the second pass escapes the ampersand the first pass introduced. Decoding runs the other way: resolve & last.
Do I need to escape accented characters as entities?
No. Modern pages are UTF-8, so é, 中 and emoji are all legal typed directly. Encoding them makes the file larger, the source unreadable, and the text unsearchable — é does not match a search for é.
Is escaping HTML enough to prevent XSS?
For text that should never be markup, yes — escaped text displays as characters and executes nothing. For content that is meant to contain markup, escaping is the wrong tool and you need a sanitiser with an allowlist of permitted elements and attributes.
Do I have to escape the greater-than sign?
Not strictly, in text content. It is escaped by convention because it costs one character, protects against a mis-parsed comment or CDATA section, and means one less rule to remember.

Related tools in Developer & Code

Browse all Developer & Code tools
The Mac mini the ecech. site is built on, beside a handwritten note reading ecech.com.

Made by one person

ecech. is not a content farm. Every tool here is written and checked by hand, one at a time, by someone who wanted the tool to exist and could not find a version that showed its working.

No accounts and no sign-in, and nothing you type reaches a server — every calculation on this page runs inside your browser. The ads are served by Google and do set their own cookies, which is set out in full on the privacy page. More about the site.