-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>
iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
Navin Rauniyar
  • 10,127
  • 14
  • 45
  • 68

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.
  • 64,864
  • 9
  • 122
  • 138