黑马node.js Day2(时钟案例代码)





    
    
    
    index首页
    



    
00
:
00
:
00

这是html素材

拆分代码以及解释如下

// 1.1导入fs模块
const fs = require('fs')
// 1.2导入path  路径处理模块
const path = require('path')

// 1.3定义正则表达式,分别匹配 标签
// 注意要用转义符  转义的杠
// \s匹配空白字符 \S匹配任意非空白字符 *表示匹配任意次
const regStyle = /', '')
    // console.log(newCSS);
    // 3.4调用fs.writeFile()方法,将提取的样式,写入 index.css文件里面
    fs.writeFile(path.join(__dirname, 'index.css'), newCSS, function (err) {
        if (err) {
            return console.log('写入index.css文件失败!', err.message);
        }
        console.log('写入index.css文件成功!');
    })
}


// 4.1定义处理js样式方法
function resolveJS(htmlStr) {
    // 4.2使用正则提取需要的内容
    const r2 = regScript.exec(htmlStr)
    // console.log(r2);
    // 4.3将提取出来的样式字符串,进行字符串的replace替换操作
    const newJS = r2[0].replace('', '')
    // console.log(newJS);
    // 4.4调用fs.writeFile()方法,将提取的样式,写入 index.js文件里面
    fs.writeFile(path.join(__dirname, 'index.js'), newJS, function (err2) {
        if (err2) {
            return console.log('写入index.js文件失败!', err2.message);
        }
        console.log('写入index.js文件成功!');
    })
}

// 5.1定义处理html样式方法
function resolveHTML(htmlStr) {
    // 5.2使用字符串的replace方法,把内嵌的