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[^>]*>([\s\S]*)<\/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)