在html中replaceurl无效

我想要replaceHTML文件中的所有链接,但这不起作用。

var fs = require('fs'); fs.readFile(__dirname + '/index.html', 'utf8', function(err, html){ if(!err){ html = html.replace('https://mysite1.github.io/', 'https://example.com/'); console.log(html); } else{console.log(err);} }); 

你能帮我吗? 我在nodejs / JavaScript中有点新鲜

replace只replace第一个实例。 您需要使用正则expression式来replace全部。

 var fs = require('fs'); fs.readFile(__dirname + '/index.html', 'utf8', function(err, html){ if(!err){ var replaceLink = "https://mysite1.github.io/"; var regex = new RegExp(replaceLink, "g"); html = html.replace(regex, "https://example.com/"); console.log(html); } else{console.log(err);} });