---
url: /docs/guide/usage/linter/rules/unicorn/prefer-string-replace-all.md
---

### 作用

在使用带有全局标志的正则表达式时，优先使用 [`String#replaceAll()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll) 而不是 [`String#replace()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace)。

### 为什么这不好？

[`String#replaceAll()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll) 方法更快也更安全，因为你不必使用正则表达式，也不必在字符串不是字面量时记得对其进行转义。而当它与正则表达式一起使用时，意图也会更加清晰。

### 示例

以下是此规则的**错误**代码示例：

```js
foo.replace(/a/g, bar);
```

以下是此规则的**正确**代码示例：

```js
foo.replace(/a/, bar);
foo.replaceAll("a", bar);
foo.replaceAll(/a|b/g, bar);

const pattern = "not-a-regexp";
foo.replace(pattern, bar);
```

## How to use

## 版本

此规则是在 v0.0.18 中添加的。

## 参考资料
