I am using Node.js to modify a html file which contains a lot:
<img src="img/scene1.jpg">
how every I want to replace this part to :
<img src="img/scene1.jpg" class="img-responsive" id="scene_141">
The id attribute should be from 1 to 141 like: id="scene_1, id="scene_2"...
I wrote a program as blow:
var fs = require('fs')
fs.readFile("my_story.html", 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
var source = data.toString();
var regexp = /<img src="img/g;
var nodeCount = (source.match(regexp) || []).length;
var count = 0;
while (count < nodeCount) {
var result = data.replace(regexp, '<img src="img/scene1.jpg" class="img-responsive" id="scene_' + count +'>');
fs.writeFile("my_story.html", result, 'utf8', function (err) {
if (err) return console.log(err);
});
count++;
}
});
However, it gives me this result:
<img src="img/scene1.jpg" class="img-responsive" id="scene_141>/scene1.jpg">
and all the id="scene_141". Anyone know how to fix it? Thank you so much in advanced!