替换 html 中的 URL 不起作用

用户3569641

我想替换 html 文件中的所有链接,但这不起作用。

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仅替换第一个实例。您需要使用正则表达式来替换所有内容。

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

});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章