Files
bun.sh/docs/guides/util/escape-html.md
Colin McDonnell 4c89c60867 Add files (#3826)
2023-07-26 14:59:39 -07:00

23 lines
599 B
Markdown

---
name: Escape an HTML string
---
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](/docs/api/utils) for more useful utilities.