zoukankan      html  css  js  c++  java
  • js分离html代码的body内外部分

    //定义网页源码

    str = '<!DOCTYPE html><html><head> <meta charset="UTF-8"></head><body style="backgroud-color:yellow"><div>文章</div></body></html>';

    //定义正则式,匹配body部分,并把body内部代码用()包起来,代表子表达式$1
    reg=/<body[^>]*>([sS]*)</body>/;

    //获取匹配结果,第二个元素刚好是子表达式$1的值
    content = str.match(reg)[1];

    输出: "<div>文章</div>"

    //获取匹配结果的外层代码
    outer = str.replace(content,'');
    输出:"<!DOCTYPE html><html><head> <meta charset="UTF-8"></head><body style="backgroud-color:yellow"></body></html>"

    //外层代码切割

    法一:

    bodyTailIndex = outer.indexOf('</body>');

    outerTail = outer.substring(bodyTailIndex);
    输出:"</body></html>"


    outerHead = outer.substring(0,bodyTailIndex);
    输出:"<!DOCTYPE html><html><head> <meta charset="UTF-8"></head><body style="backgroud-color:yellow">"

    法二:

    outerPart = str.split(content)

  • 相关阅读:
    linux文件操作
    文件和目录维护
    随心而记
    开学了
    基本逻辑门电路原理
    ubuntu搭建ftp服务
    中断
    数码管
    字,寻址和移位
    Error: Could not find or load main class resourcemanager
  • 原文地址:https://www.cnblogs.com/hdwang/p/8882499.html
Copyright © 2011-2022 走看看