Decode numeric HTML entities

Turn é and é style references into characters.

JavaScript Version 1.0.0 · MIT · .md

decode-html-entities.js
function decodeNumericEntities(text) { return text.replace(/&#(x[0-9a-f]+|\d+);/gi, (entity, code) => { const point = code[0].toLowerCase() === 'x' ? parseInt(code.slice(1), 16) : Number(code); return point <= 0x10ffff ? String.fromCodePoint(point) : entity; }); }

Input & expected output

Input

"Caf&#233; &#x1F600;"

Expected output

"Café 😀"

The edge case

Input

"&#1114112;"

Expected output

"&#1114112;"

Know the limits

  • Numeric references only; named ones such as &eacute; need an HTML parser. The original regex mishandled hexadecimal references.
  • Out-of-range code points are left unchanged.

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 Decode HTML entities from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.

Rewritten for decimal and hexadecimal numeric references with a code-point range check; the original regex mishandled hexadecimal references. 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”.