Regular expression HTML processing is never recommended, and there are many arguments ( 1 , 2 , 3 ).
The most popular and reliable way to process an HTML source is to create a model of the source document. JSDOM is a single node.js module that provides a good DOM API. Here is a demonstration of how to solve your case with JSDOM:
var fs = require("fs"); var gulp = require("gulp"); var dom = require("jsdom"); var domSerialiser = dom.serializeDocument; var input = "input.html"; var output = "output.html"; gulp.task("copy-html", function () { var extractionPoint = "#extraction-location"; var insertionPoint = "#insertion-location"; extractFrom(input, extractionPoint). then(insertInto.bind(null, output, insertionPoint)). then(saveOutput); }); function extractFrom(file, section) { return new Promise(function (resolve, reject) { dom.env({ file: file, done: function (error, window) { var portion; if (error) { reject(error); return; } portion = window.document.querySelector(section); resolve(portion.outerHTML); } }); }); } function insertInto(file, section, portion) { return new Promise(function (resolve, reject) { dom.env({ file: file, done: function (error, window) { if (error) { reject(error); return; } section = window.document.querySelector(section);
Hope the above example provides a good basis for resolving your specific issue.
source share