> 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/replace.md).

# \_.replace（字符串替换）

**语法：**

```javascript
_.replace([string=''], pattern, replacement)
```

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

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

**描述：**

替换`string`字符串中匹配的`pattern`为给定的`replacement`。

> **注意:**&#x8FD9;个方法基于[`String#replace`](https://mdn.io/String/replace)。
>
> 可使用正则替换。

**开始版本：**&#x34;.0.0

**参数：**

* `[string=''] (string)`: 待替换的字符串。
* `pattern (RegExp|string)`: 要匹配的内容。
* `replacement (Function|string)`: 替换的内容。

**返回值：**

* `(string)`: 返回替换后的字符串

**例子：**

```javascript
_.replace('Hi Fred', 'Fred', 'Barney');
// => 'Hi Barney'
```
