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