JavaScript:
搜索和替换文本

How to: (如何操作:)

// 搜索文本并替换
let text = "Hello World! Programming is fun.";
let newText = text.replace("World", "Mandarin Reader");

console.log(newText); // 输出: Hello Mandarin Reader! Programming is fun.
// 使用正则表达式全局替换文本
let text = "Apples are round, and apples are juicy.";
let newText = text.replace(/apples/gi, "oranges");

console.log(newText); // 输出: Oranges are round, and oranges are juicy.

Deep Dive (深入了解)

搜索和替换文本的需求诞生于早期编程的日子,当时通过批处理命令和基本的编辑器实现。现今,JavaScript 中提供了 .replace() 方法用于文本替换。这个方法可接受字符串或正则表达式作为搜索参数,实现灵活的文本操作。除了 .replace(), 替代方法如使用 .split().join() 组合替换也存在,但 .replace() 更直观、方便。

在处理复杂模式时,正则表达式(RegEx)变得无比强大。使用正则表达式的 g 标志进行全局搜索和替换,以及 i 标志忽略大小写。值得注意的是,.replace() 方法默认只替换第一个匹配项,除非使用正则表达式的全局 (g) 标志。

See Also (另请参阅)