-4

If I use the following then it is displayed like html:

<h1>heading</h1>

This will be the result:

heading

But how do I unescape all the characters inside <code>?

Which results exactly what inside the <code>:

<h1>heading</h1>
Gone Coding
  • 90,552
  • 24
  • 176
  • 195
Navin Rauniyar
  • 9,427
  • 12
  • 41
  • 67

1 Answers1

0

Either replace the tag syntax with HTML entities

str = '<foo>';
str = str.replace(/</g, '&lt;').replace(/>/g, '&gt;');
str; // "&lt;foo&gt;"

Or insert it into the page explicitly as text, e.g.

parent.appendChild(document.createTextNode(str));

You may also wish to escape single and double quotes to be on the safe side if you plan to use random strings in attributes, too.

str = str.replace(/"/g, '&quot;').replace(/'/g, '&#39;');
Paul S.
  • 61,621
  • 8
  • 116
  • 132