Unescape HTML special characters

Turn & < > " and ' back into characters in one pass.

JavaScript Version 1.0.0 · MIT · .md

unescape-html-special-characters.js
function unescapeHtml(text) { return text.replace(/&(?:amp|lt|gt|quot|#0*39);/g, entity => ({ '&amp;': '&', '&lt;': '<', '&gt;': '>', '&quot;': '"' })[entity] ?? "'"); }

Input & expected output

Input

"&lt;b&gt;Ana &amp; Rui&lt;/b&gt;"

Expected output

"<b>Ana & Rui</b>"

The edge case

Input

"&amp;lt;"

Expected output

"&lt;"

Know the limits

  • The original replaced entities one after another, so "&amp;lt;" became "<". A single pass keeps it as "&lt;".

The function above is the implementation used by the lab. The lab validates JSON types and caps size before execution; include equivalent validation when accepting untrusted input in your application.

Source & maintenance

Based on Unescape HTML special characters from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.

Single pass, so already-escaped entities are not unescaped twice as in the original sequence of replacements. Original MDX retained as inert source evidence.

Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.

Local fixture scope · Node.js 22.23.2 · reviewed October 28, 2026. No external service is required.

Keep exploring

Type two or more characters. Try “duplicate”, “webhook” or “retry”.