<p id="topnav">
//新修改的代码
</p>
<p id="topnav">
//以前的代码
</p>
有个主页的顶部导航和其它的很多子页的顶部导航是相同的,现在修改了主页的顶部导航代码,子页的也要改了。可是一个一个地改实在是累死我了。有什么工具可以帮我完成这个工作吗?
PHP中文网2017-04-17 15:43:38
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
巴扎黑2017-04-17 15:43:38
A text editor is enough. It is the software that replaces the native text editor under win
ringa_lee2017-04-17 15:43:38
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);
}