I need to read a file and replace some texts in this file with dynamic content. When I tried string.replace, it does not work for the data that I read from the file. But for the string, it works. I am using node.js and express.
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); } });
Exit:
temp: Hello Myname, would you like some tea? msg: Hello %NAME%, would you like some %DRINK%?
source share