使用给定的参数对句子执行一次查找和替换,然后返回新句子。
第一个参数为搜索字符串,第二个参数为搜索目标,第三个参数为替换字符串 。
注意:第二个参数首字母大写,第三个参数首字母也要大写。
function myReplace(str, before, after) {
if (before.charAt(0).toUpperCase() === before.charAt(0)) {
after = after.charAt(0).toUpperCase() + after.substr(1);
}
return str.replace(before, after);
}
myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");