首页 > 其他分享 >文件系统模块3(拆分文件案例)

文件系统模块3(拆分文件案例)

时间:2022-10-19 01:23:06浏览次数:46  
标签:fs console 拆分 err 文件系统 replace 模块 const log

// 导入
const fs = require('fs')
const path = require('path')
// 匹配正则表达式
// 样式
const regStyle = /<style>[\s\S]*<\/style>/
// js
const regScript = /<script>[\s\S]*<\/script>/
fs.readFile(path.join(__dirname, '/拆分html.html'), 'utf8', (err, dataStr) => {
    if (err) return console.log('读取失败' + err.message)
    // 读取成功后拆分css,js,html文件
    resolveCSS(dataStr)
    resolveJS(dataStr)
    resolveHTML(dataStr)
})
// exec()方法:检索正则表达式的匹配方法

// 定义处理css样式的方法
function resolveCSS(htmlStr) {
    const r1 = regStyle.exec(htmlStr)
    // console.log(r1)
    // 将提取出来的样式进行replace替换操作
    const newCSS = r1[0].replace('<style>','').replace('</style>','')
    // console.log(newCSS)
    // 调用fs.writeFile提取写入
    fs.writeFile(path.join(__dirname,'/day/day3/拆分.css'),newCSS,function(err){
        if(err)return console.log('写入样式失败'+err.message)
        console.log('写入样式文件成功 ');
    })
}
// 定义处理js样式的方法
function resolveJS(htmlStr) {
    const r2 = regScript.exec(htmlStr)
    // console.log(r2)
    // 将提取出来的样式进行replace替换操作
    const newJS = r2[0].replace('<script>','').replace('</script>','')
    // console.log(newJS)
    // 调用fs.writeFile提取写入
    fs.writeFile(path.join(__dirname,'/day/day3/拆分.js'),newJS,function(err){
        if(err)return console.log('写入js样式失败'+err.message)
        console.log('写入js文件成功 ');
    })
}
// 定义处理html样式的方法
function resolveHTML(htmlStr) {
    const newhtml = htmlStr.replace(regStyle,'<link rel="stylesheet" href="./day/day3/拆分.css">').replace(regScript,'<script src="./day/day3/拆分.js"></script>')
    fs.writeFile(path.join(__dirname,'/day/day3/拆分.html'),newhtml,function(err){
        if(err)return console.log('写入html失败'+err.message)
        console.log('写入html文件成功 ');
    })
}

  

标签:fs,console,拆分,err,文件系统,replace,模块,const,log
From: https://www.cnblogs.com/wencaiguagua/p/16804794.html

相关文章

  • 文件系统模块2(path模块化,)
    1,拼接路径constfs=require('fs')constpath=require('path')fs.readFile(path.join(__dirname,'path'),'utf8',function(err,dataStr){if(err){return......
  • 文件系统模块1(导入,写入)
    1导入fs模块constfs=require('fs')2,读取文件内容fs.readFlie(path,'utf8',function(err,dataStr){console.log(err)console.log('-----')console.log(dataStr)......
  • Python 操作Excel-openpyxl模块使用
    openpyxl的用法实例1.1Openpyxl库的安装使用openpyxl模块是一个读写Excel2010文档的Python库,如果要处理更早格式的Excel文档,需要用到额外的库,openpyxl是一......
  • 模块
    索引取值与迭代取值的差异索引取值可以按照任意索引位任意次数取值。不支持无序数据类型。迭代取值只能从前往后依此取值无法返回,支持所有数据类型。模块模块简......
  • python基础入门之模块
    python基础入门之模块索引取值与迭代取值的差异l1=[11,22,33,44,55]1.索引取值print(l1[2])#33 可以任意位置任意次数取值,不支持无序类型的数据取值。2.迭......
  • 模块
    今日内容索引取值与迭代取值的差异1.索引取值可以在任意位置任意次数取值不支持无序类型数据取值2.迭代取值 只能从前往后依次取值并且无法退后 支持所有类......
  • 模块的导入相关知识
    昨日内容回顾异常处理的语法结构try: 下跟被监测的子代码except: 后跟错误类型子代码为该类型错误解决的提示else: 被监测的子代码正常运行时运行的代码fin......
  • 索引取值与迭代取值,模块基础
    目录索引取值与迭代取值,模块基础今日内容概要今日内容详细索引取值与迭代取值的差异模块简介模块的分类导入模块的两种句式导入模块补充说明循环导入问题判断文件类型模块......
  • 模块基础了解
    今日内容详细索引取值与迭代取值的差异l1=[11,22,33,44,55]1.索引取值 可以任意位置任意次数取值 不支持无序类型的数据取值2.迭代取值 只能从前往后依次取值......
  • python模块/导入模块
    索引取值与迭代取值的差异l1=[1,2,3,4,5]1.索引取值可以任意位置任意次数的取值不支持无序类型的数据取值print(l1[3])print(l1[3])#可以直接获取任意位置的......