> For the complete documentation index, see [llms.txt](https://lodash.shujuwajue.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lodash.shujuwajue.com/string/escape.md).

# \_.escape（转html实体字符）

**语法：**

```javascript
_.escape([string=''])
```

**源代码链接：**[source](https://github.com/lodash/lodash/blob/4.17.10/lodash.js#L14250)

**npm包链接：**[npm package](https://www.npmjs.com/package/lodash.escape)

**描述：**

转义string中的 "&", "<", ">", '"', "'", 和 "\`" 字符为HTML实体字符。

> **注意:**&#x4E0D;会转义其他字符。如果需要，可以使用第三方库，例如[*he*](https://mths.be/he)。

虽然 ">" 是对称转义的，字符如 ">" 和 "/" 没有特殊的意义，所以不需要在 HTML 转义。 除非它们是标签的一部分，或者是不带引号的属性值。 查看[Mathias Bynens's article](https://mathiasbynens.be/notes/ambiguous-ampersands)的文&#x7AE0;*(under "semi-related fun fact")*&#x4E86;解详情 。

当解析 HTML 时，总应该在[属性值上使用引号](http://wonko.com/post/html-escaping)以减少 XSS 的可能性。

**开始版本：**&#x30;.1.0

**参数：**

* `[string=''] (string)`: 要转义的字符串。&#x20;

**返回值：**

* `(string)`: 返回转义后的字符串。

**例子：**

```javascript
_.escape('fred, barney, & pebbles');
// => 'fred, barney, &amp; pebbles'
```
