代码示例:(标识:js_string_replaceall_2)
<!DOCTYPE html>
<html>
<body>

<h1>JavaScript 字符串</h1>

<h2>replaceAll() 方法</h2>

<p>ES2021 引入了字符串方法 replaceAll():</p>

<p id="demo"></p>

<script>
let text = "I love cats. Cats are very easy to love. Cats are very popular";
text = text.replaceAll(/Cats/g,"Dogs");
text = text.replaceAll(/cats/g,"dogs");

document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>
运行结果: