Skip to content

Latest commit

 

History

History
27 lines (23 loc) · 689 Bytes

unescapeHTML.md

File metadata and controls

27 lines (23 loc) · 689 Bytes
title tags
unescapeHTML
string,browser,beginner

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 =>
  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>'