JS không có hàm replace all built-in, tự viết hàm replaceYesWithHi(word, sentence, replaceValue) dùng split().join().
Mã thực hiện
var sentence = "Yes, My Name is John Smith. I live in US. Yes, My Favourite Subject is JavaScript";
function replaceYesWithHi(word, sentence, replaceValue) {
return sentence.split(word).join(replaceValue);
}
console.log(replaceYesWithHi("Yes", sentence, "Hi"));
Kết quả
Hi, My Name is John Smith. I live in US. Hi, My Favourite Subject is JavaScript