【问题标题】:How to separate out and save html files which are merged in a single file如何分离并保存合并在单个文件中的html文件
【发布时间】:2020-08-22 03:59:13
【问题描述】:

我有几个 html 文件,每个文件都合并了 8-10 个 html 页面。所有这些都由页面/文件的名称分隔,后跟 记。

如何将这些单独的页面/文件分开并用各自的名称保存?

【问题讨论】:

  • 你不能用手来做吗?
  • @Konowy,实际上有 100 个这样的文件。
  • 如果你给出这个文件的例子会更容易。也许在 pastebin 上。
  • 不确定我是否正确。我在这里粘贴了 HTML 内容pastebin.com/10ahGC0Q 请看里面有 2 个 HTML。我需要将这 2 个分开并保存为 2 个不同的 HTML 文件。最好希望文件名与 TML 中提到的字符串一致。

标签: javascript html file


【解决方案1】:

我写了nodejs程序来解决你的问题:

const fs = require('fs')

const fileName = process.argv.slice(-1)[0]
const file = fs.readFileSync(fileName, 'utf-8')

let fileRest = file

const htmlEndTag = '</html>'

while (true) {
  const end = fileRest.indexOf(htmlEndTag)
  if (end === -1) {
    break
  }
  const oneFile = fileRest.slice(0, end + htmlEndTag.length)
  const match = oneFile.match(/\(([a-zA-Z0-9_-]+)\)/)
  const name = match[1]
  const content = oneFile.slice(match[0].length + match.index)

  fileRest = fileRest.slice(oneFile.length)
  fs.writeFile(name + '.html', content, () => {
    console.log('Done writing', name)
  })
}

【讨论】:

    猜你喜欢
    • 2018-06-03
    • 1970-01-01
    • 2010-12-25
    • 1970-01-01
    • 2023-03-27
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    • 2023-02-11
    相关资源
    最近更新 更多