【发布时间】:2021-01-23 14:08:28
【问题描述】:
我的代码是这样的..
function replaceAll(str, from, to) {
let result = ''
for(let i = 0 ; i < str.length ; i++)
if(str[i]===from) {
result = str.replace(from,to)
}
}
return result;
}
我想就这样回来
let output = replaceAll('loop', 'o', 'e');
console.log(output); // --> 'leep'
但它只是改变了'leop'
【问题讨论】:
-
很不清楚,为什么你需要一个循环。
-
@NinaScholz 来处理
replace(string, ...)仅替换该字符串的第一次出现的事实。但是整个构造很麻烦,只有from.length === 1
标签: javascript replace