JavaScript 有一些東西跟後端不太一樣,需要特別注意
string.replace()
字串replace 只會取代第一個匹配的,如果要取代一個字串裡的所有關鍵字需要使用 regex, 也可以使用 replaceAll()
let text = "Please visit Microsoft and Microsoft!";
let newText = text.replace("Microsoft", "W3Schools");
console.log(newText); // print: Please visit W3Schools and Microsoft!
---
let text = "Please visit Microsoft and Microsoft!";
let newText = text.replace(/Microsoft/g, "W3Schools"); // /g 代表global match
console.log(newText); // print: Please visit W3Schools and W3Schools!