Skip to main content
File

title: unescapeHTML tags: string,browser,beginner

TS JS Deno

Unescapes escaped HTML characters.

Use String.prototype.replace() with a regex that matches the characters that need to be unescaped, using a callback function to replace each escaped character instance with its associated unescaped character using a dictionary (object).

const unescapeHTML = (str: string) =>
  str.replace(
    htmlUnEscapeReg,
    (tag: string) => (HTMLUnEscapeChars as StringMap<string>)[tag] || tag
  );
unescapeHTML("&lt;a href=&quot;#&quot;&gt;Me &amp; you&lt;/a&gt;"); // '<a href="#">Me & you</a>'