Dev HelpersOpen library
← Back

Escape HTML text

Escape HTML text. A self-contained example using the JavaScript runtime.

Open this example in the interactive library

Source code

const entities = {
  "&": "&",
  "<": "&lt;",
  ">": "&gt;",
  '"': "&quot;",
  "'": "&#39;",
};
console.log("<b>safe & sound</b>".replace(/[&<>"']/g, (c) => entities[c]));

Sample output

&lt;b&gt;safe &amp; sound&lt;/b&gt;