Files
bun.sh/docs/guides/util/escape-html.mdx
2025-11-21 14:06:19 -08:00

25 lines
638 B
Plaintext

---
title: Escape an HTML string
sidebarTitle: Escape HTML
mode: center
---
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.
```ts
Bun.escapeHTML("<script>alert('Hello World!')</script>");
// &lt;script&gt;alert(&#x27;Hello World!&#x27;)&lt;&#x2F;script&gt;
```
---
See [Docs > API > Utils](/runtime/utils) for more useful utilities.