📌  相关文章
📜  javascript 替换字符串 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:36.959000             🧑  作者: Mango

代码示例6
const p = 'dog dog cat rat';
const regex = /dog/gi;

console.log(p.replace(regex, 'cow'));
//if pattern is regular expression all matches will be replaced
//output: "cow cow cat rat"

console.log(p.replace('dog', 'cow'));
// If pattern is a string, only the first occurrence will be replaced.
//output: "cow dog cat rat"