Tuesday 9 May 2017

How to inser variable in .replace for fs in nodejs

How will I gonna pass the ref variable inside the replace()? I try it my code below, b

var ref = "www.facebook.com";

fs.readFile('text.html','utf8', function (err, data) {
    if (err) {
        return console.log(err);
    }

    var result = data.replace(/href="'+ref+'"/g, 'href="changeRef"');

    fs.writeFile('text.html', result, 'utf8', function (err) {
        if (err) return console.log(err);
        next();
    });

})



via JerVi

No comments:

Post a Comment