unescape()
unescape(
str):string
Unescapes HTML entities to their corresponding characters.
Unescapes &, <, >, ", and '.
Parametersβ
str: stringβ
The string to unescape.
Returns: stringβ
The unescaped string.
See Alsoβ
Sinceβ
2.0.0
Performanceβ
O(n) time where n is string length. Single regex pass with object lookup.
Also known asβ
unescape (Lodash, es-toolkit) Β· unescapeHtml (Modern Dash) Β· β (Remeda, Radashi, Ramda, Effect, Antfu)
Exampleβ
unescape('<div>'); // => '<div>'
unescape('a & b'); // => 'a & b'
unescape('"hello"'); // => '"hello"'
unescape(''quoted''); // => "'quoted'"
// Roundtrip with escape
const original = '<script>alert("XSS")</script>';
unescape(escape(original)) === original; // => true
How it works?β
Converts HTML entities back to their corresponding characters.
Inverse of escape.
Entity Mappingβ
| Entity | Character |
|---|---|
& | & |
< | < |
> | > |
" | " |
' | ' |
Use Caseβ
Use Casesβ
Decode API responses for display πβ
Convert HTML entities from API content back to readable characters. Essential when displaying user-generated content stored with HTML encoding.
unescape("Tom & Jerry");
// => "Tom & Jerry"
Process CMS content for rendering πβ
Restore original characters from CMS or WYSIWYG editor output. Critical for headless CMS integrations where content comes pre-escaped.
unescape("<p>Welcome!</p>");
// => "<p>Welcome!</p>"
Roundtrip with escape for data integrityβ
Verify data integrity by ensuring escape/unescape roundtrips preserve content.
const original = '<script>alert("XSS")</script>';
unescape(escape(original)) === original; // => true