> ## Documentation Index
> Fetch the complete documentation index at: https://bun.zhcndoc.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 转义 HTML 字符串

`Bun.escapeHTML()` 工具可用于转义字符串中的 HTML 字符。以下字符将被替换。

* `"` 变为 `"&quot;"`
* `&` 变为 `"&amp;"`
* `'` 变为 `"&#x27;"`
* `<` 变为 `"&lt;"`
* `>` 变为 `"&gt;"`

该函数针对大输入进行了优化。非字符串类型会先转换为字符串再进行转义。

```ts theme={"theme":{"light":"github-light","dark":"dracula"}}
Bun.escapeHTML("<script>alert('Hello World!')</script>");
// &lt;script&gt;alert(&#x27;Hello World!&#x27;)&lt;&#x2F;script&gt;
```

***

更多实用工具请参见 [文档 > API > Utils](/runtime/utils)。
