css - 怎样批量替换目录下的所有html文件的指定部分?
天蓬老师
天蓬老师 2017-04-17 15:41:38
0
6
426
<p id="topnav">
    //新修改的代码
</p>
<p id="topnav">
    //以前的代码
</p>

有个主页的顶部导航和其它的很多子页的顶部导航是相同的,现在修改了主页的顶部导航代码,子页的也要改了。可是一个一个地改实在是累死我了。有什么工具可以帮我完成这个工作吗?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(6)
Ty80

Since it is a front-end, you can use node to write a script, loop through each file in the directory, and use regular expressions to replace it

So it is to read the file content in a loop, find the content, extract this content, replace, update and save, it doesn’t matter what language is used

PS: If you had maintained good code organization habits before, this problem would not have occurred. For example, the top navigation was made into a fragment and introduced where needed. In this way, you only need to modify one place in the future, so When you find that you are reusing a long section of code, it is best to split it into a separate file

巴扎黑

A text editor is enough. It is the software that replaces the native text editor under win

刘奇

Shouldn’t the same code reference the same template?

大家讲道理

sublime

Peter_Zhu

Sublime Text full text search regular replacement

左手右手慢动作

I simply wrote a loop replacement script, I hope it can help you.
You need to use nodejs.
Install Node.js


The code is as follows, filesArr is the file array that needs to be replaced, and XXX is your newly modified code:

var fs = require('fs');
var filesArr = [
    'header.html',
    'child1/nav.html',
    'child2/nav.html',
    'child3/nav.html'
];
for(var i = 0; i < filesArr.length; i++) {
    var fileContent = fs.readFileSync(filesArr[i], 'utf-8');
    var newCon = fileContent.replace(/(<p id=\"topnav\">[\r\n]*)[\s\S]*([\r\n]*<\/p>)/, "XXX\r\n");
    fs.writeFile(filesArr[i], newCon);
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template