2017-04-07 10 views
1

htmlファイル内のすべてのリンクを置換したいが、これは機能しません。htmlでのURLの置換が機能しない

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

}); 

uがこれで私を助けることができますか?私はnodejs/JavaScriptで少し新しいです

+0

'replace'文の前に' html'の値は何ですか? – gurvinder372

+0

https://pastebin.com/V1VukHVBペーストビンに追加しました – user3569641

答えて

1

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

}); 
関連する問題