Node.js 04-综合案例:切分网页

Node.js-综合案例-切分网页

原文链接:Blogs Content

此案例用于对html的切分尝试

需要的知识:

  • 对html基础知识掌握

  • 正则表达式

  • FS模块和Path模块

const fs = require('fs');
const path= require('path');

//正则
const regStyle = /<style>[\s\S]*<\/style>/ ;
const regScript = /<script>[\s\S]*<\/script>/ ;

fs.readFile(path.join(__dirname,'/例子.html'),'utf-8',function(err,dataStr){
    if(err) return console.log('False!');
    //传递参数到提取CSS的方法
    resolveCSS(dataStr);
    resolveJS(dataStr);
    resolveHTML(dataStr);
})

//取CSS代码
function resolveCSS(htmlStr){
    const r1 = regStyle.exec(htmlStr);
    //exec方法提取的默认是以数组形式存在,匹配到的在 数组【0】为html代码位置
    const newCSS = r1[0].replace('<style>','').replace('</style>','')//替换成为纯代码
    console.log(newCSS);
}
//取JS代码
function resolveJS(htmlStr){
    const r1 = regScript.exec(htmlStr);
    //exec方法提取的默认是以数组形式存在,匹配到的在 数组【0】为html代码位置
    const newCSS = r1[0].replace('<script>','').replace('</script>','')//替换成为纯代码
    console.log(newCSS);
}
//定义处理html结构的方法
function resolveHTML(htmlStr){
    //优化结构,将固定的局内JS和CSS语句定义成外部JS和CSS
    const newHtml = htmlStr.replace(regStyle,'<link rel="stylesheet" href="./index.css" />').replace(regScript,'<script src="./index.js"</script>');
    console.log(newHtml);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值