Skip to content
NewBun 1.4.5 is out. Release notes
AphrodyBun

Guides · Utilities

Escape an HTML string

Escape ", &, ', < and > characters in a string with Bun.escapeHTML().

Bun.escapeHTML() escapes HTML characters in a string. It makes the following replacements.

  • " becomes "&quot;"
  • & becomes "&amp;"
  • ' becomes "&#x27;"
  • < becomes "&lt;"
  • > becomes "&gt;"

This function is optimized for large input. It converts non-string values to a string before escaping them.

Bun.escapeHTML("<script>alert('Hello World!')</script>");
// &lt;script&gt;alert(&#x27;Hello World!&#x27;)&lt;/script&gt;

See Utils.