25 lines
568 B
Plaintext
25 lines
568 B
Plaintext
---
|
|||
|
|
title: Escape an HTML string
|
||
|
|
sidebarTitle: Escape HTML
|
||
|
|
mode: center
|
||
|
|
---
|
||
|
|
|
||
|
|
`Bun.escapeHTML()` escapes HTML characters in a string. It makes the following replacements.
|
||
|
|
|
||
|
|
- `"` becomes `"""`
|
||
|
|
- `&` becomes `"&"`
|
||
|
|
- `'` becomes `"'"`
|
||
|
|
- `<` becomes `"<"`
|
||
|
|
- `>` becomes `">"`
|
||
|
|
|
||
|
|
This function is optimized for large input. It converts non-string values to a string before escaping them.
|
||
|
|
|
||
|
|
```ts
|
||
|
|
Bun.escapeHTML("<script>alert('Hello World!')</script>");
|
||
|
|
// <script>alert('Hello World!')</script>
|
||
|
|
```
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
See [Utils](/runtime/utils).
|