String 对象-->replace() 方法

1.定义和用法

  replace() 方法用于字符串替换

  语法:

    string.replace(searchvalue,newvalue)

    参数:

      searchvalue:被替换的字符串

      newvalue:新字符串

  注意:此时只能替换第一次出现的字符串,如果想要全替换需要正则

  举例:将段落中的“blue”替换成“red”。

var str='Mr Blue has a blue house and a blue car'
console.log(str.replace(/blue/g,"red"))

  输出:

原文地址:https://www.cnblogs.com/abner-pan/p/12701144.html