search

Home  >  Q&A  >  body text

css - 怎样批量替换目录下的所有html文件的指定部分?

<p id="topnav">
    //新修改的代码
</p>
<p id="topnav">
    //以前的代码
</p>

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

天蓬老师天蓬老师2786 days ago427

reply all(6)I'll reply

  • PHP中文网

    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

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 15:43:38

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

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 15:43:38

    Shouldn’t the same code reference the same template?

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 15:43:38

    sublime

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 15:43:38

    Sublime Text full text search regular replacement

    reply
    0
  • ringa_lee

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

    reply
    0
  • Cancelreply