Unescape HTML

JavaScript, String, Regexp · Oct 22, 2020

Unescapes escaped HTML characters.

  • Use String.prototype.replace() with a regexp that matches the characters that need to be unescaped.
  • Use the function's callback to replace each escaped character instance with its associated unescaped character using a dictionary (object).
const unescapeHTML = str =>
  str.replace(
    /&|<|>|'|"/g,
    tag =>
      ({
        '&': '&',
        '&lt;': '<',
        '&gt;': '>',
        '&#39;': "'",
        '&quot;': '"'
      }[tag] || tag)
  );
unescapeHTML('&lt;a href=&quot;#&quot;&gt;Me &amp; you&lt;/a&gt;');
// '<a href="#">Me & you</a>'

More like this

  • Escape HTML

    Escapes a string for use in HTML.

    JavaScript, String · Oct 13, 2021

  • String is anagram

    Checks if a string is an anagram of another string (case-insensitive, ignores spaces, punctuation and special characters).

    JavaScript, String · Oct 20, 2020

  • Remove non ASCII characters

    Removes non-printable ASCII characters.

    JavaScript, String · Oct 22, 2020