Runtime
Bundler
Package Manager
Test Runner
Guides
Reference
Blog
Install Bun
Guides Utilities

Escape an HTML string

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

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

This function is optimized for large input. Non-string values are converted to a string before escaping.

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

See Utils.

On this page

No Headings