我需要读取一个文件并用动态内容替换该文件中的一些文本。当我尝试 string.replace 时,它不适用于我从文件中读取的数据。但是对于它正在工作的字符串。我正在使用节点。 js和快递。
fs.readFile('test.html', function read(err, data) {
if (err) {
console.log(err);
}
else {
var msg = data.toString();
msg.replace("%name%", "myname");
msg.replace(/%email%/gi, 'example@gmail.com');
temp = "Hello %NAME%, would you like some %DRINK%?";
temp = temp.replace(/%NAME%/gi,"Myname");
temp = temp.replace("%DRINK%","tea");
console.log("temp: "+temp);
console.log("msg: "+msg);
}
});
输出:
temp: Hello Myname, would you like some tea?
msg: Hello %NAME%, would you like some %DRINK%?