I'm trying to create a tool that will allow me to quickly update several text strings in an html file. I'm doing this by searching for the contents of tags with specific ids and then replacing their text content. I've got much of it working but I'm not sure how to enable this to write multiple replacements into the file. As it is, it's simply running only the last of my replacements.
Here's what I have:
var fs = require('fs')
fs.readFile('sample.html', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
var title1 = "boogers"
var text1 = "ronald mcdonald";
var result = data.replace(/<div id="text1">(.*)<\/div>/g, '<div id="text1">' + text1 + '</div>');
var result = data.replace(/<title>(.*)<\/title>/g, '<title>' + title1 + '</title>');
fs.writeFile('sample.html', result, 'utf8', function (err) {
if (err) return console.log(err);
});
});
In the code above, it will replace the title's content but not the div tag's. I imagine this is a pretty basic fix but I'm new to node js and javascript. Thanks for any help.
sample.htmland the expected output from it?