Bun

GuidesUtilities

Escape an HTML string with Bun

The Bun.escapeHTML() utility can be used to escape HTML characters in a string. The following replacements are made.

  • " becomes """
  • & becomes "&"
  • ' becomes "'"
  • < becomes "&lt;"
  • > becomes "&gt;"

This function is optimized for large input. Non-string types will be converted to a string before escaping.

Bun.escapeHTML("<script>alert('Hello World!')</script>");
// &lt;script&gt;alert(&#x27;Hello World!&#x27;)&lt;&#x2F;script&gt;
"); // <script>alert('Hello World!')</script>`).then(()=>{clicker.querySelector(".clipboard").classList.add("hidden");clicker.querySelector(".check").classList.remove("hidden");setTimeout(()=>{clicker.querySelector(".clipboard").classList.remove("hidden");clicker.querySelector(".check").classList.add("hidden")},2500)})})});

See Docs > API > Utils for more useful utilities.