Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
88
rated 0 times [  91] [ 3]  / answers: 1 / hits: 20294  / 12 Years ago, tue, november 20, 2012, 12:00:00

I need to read a file and replace some texts in that file with dynamic content.when i tried string.replace it is not working for the data that i read from the file.But for the string it is working.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, '[email protected]');

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);
}
});


Output:



temp: Hello Myname, would you like some tea?
msg: Hello %NAME%, would you like some %DRINK%?

More From » string

 Answers
12
msg = msg.replace(/%name%/gi, myname);


You're passing a string instead of a regex to the first replace, and it doesn't match because the case is different. Even if it did match, you're not reassigning this modified value to msg. This is strange, because you're doing everything correctly for tmp.


[#81900] Sunday, November 18, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dannyc

Total Points: 517
Total Questions: 106
Total Answers: 116

Location: Netherlands
Member since Mon, Jun 7, 2021
3 Years ago
;