- 请将时钟代码分离html,css,js,时钟html文件名为dataTime下的index.html
// 案例时钟
// 1.读取时钟文件
const fs = require("fs");
const path = require("path");
fs.readFile("./dataTime/index.html","utf-8",function(err,user){
if(err){
return console.log("获取文件失败"+err.message);
}
// console.log(user);
resolveCss(user);
resolveJs(user);
resolveHtml(user);
})
// 修改html
function resolveHtml(user){
let patt = /<style>[\s\S]*<\/style>/;
let requsCss = patt.exec(user);
user = user.replace(requsCss[0],"<link rel='stylesheet' href='./dataTime/index.css'>")
let patts = /<script>[\s\S]*<\/script>/;
let requsJs = patts.exec(user);
user = user.replace(requsJs[0],"<script src='./dataTime/index.js'></script>")
fs.writeFile("./dataTime/index.html",user,"utf-8",function(err){
if(err){
return console.log("创建文件失败"+err.message);
}
})
}
// 分离js
function resolveJs(user){
// 2.正则匹配style内容
let patt = /<script>[\s\S]*<\/script>/;
let requsCss = patt.exec(user);
let textStyle = requsCss[0].replace("<script>","").replace("</script>","")
// 3.创建css文件
fs.writeFile("./dataTime/index.js",textStyle,"utf-8",function(err){
if(err){
return console.log("创建文件失败"+err.message);
}
})
}
// 分离style
function resolveCss(user){
// 2.正则匹配style内容
let patt = /<style>[\s\S]*<\/style>/;
let requsCss = patt.exec(user);
let textStyle = requsCss[0].replace("<style>","").replace("</style>","")
// 3.创建css文件
fs.writeFile("./dataTime/index.css",textStyle,"utf-8",function(err){
if(err){
return console.log("创建文件失败"+err.message);
}
})
}