<script> var str='<p>'; str='<table cellspacing="0" helvetica="" sans=""><tr><td colspan="8" rowspan="5">'; str+='<td style = "color:blue" colspan="8" rowspan="5">'; str+='<td><span><a href="http://www.17ce.com/site/dns/20200408_3875c5d0799211eab8bc2bbb7b33a12c:1.html#s159015" >GET</a> <a href="http://www.17ce.com/site/dns/20200408_3875c5d0799211eab8bc2bbb7b33a12c:1.html#s159015" >PING</a> <a href="http://www.17ce.com/site/dns/20200408_3875c5d0799211eab8bc2bbb7b33a12c:1.html#s159015" >Trace</a> <a href="http://www.17ce.com/site/dns/20200408_3875c5d0799211eab8bc2bbb7b33a12c:1.html#s159015" >Dig</a></span></td>'; str+='<td colspan="8" rowspan="5"><p>jb51.net</p></td>'; str+='<td colspan="8" rowspan="5"></td></tr></table>'; str+='<table><tr style = "color:bluefffffffffffffffffffffffffffffffffffffffffff"><td colspan="8" rowspan="5">'; str+='<tr> <td colspan="16"></td> </tr>'; str+='<td><span><a href="http://www.17ce.com/site/dns/20200408_3875c5d0799211eab8bc2bbb7b33a12c:1.html#s159015" >GET</a> <a href="http://www.17ce.com/site/dns/20200408_3875c5d0799211eab8bc2bbb7b33a12c:1.html#s159015" >PING</a> <a href="http://www.17ce.com/site/dns/20200408_3875c5d0799211eab8bc2bbb7b33a12c:1.html#s159015" >Trace</a> <a href="http://www.17ce.com/site/dns/20200408_3875c5d0799211eab8bc2bbb7b33a12c:1.html#s159015" >Dig</a></span></td>'; str+='<td colspan="8" rowspan="5"><p>jb51.net</p></td>'; str+='<td colspan="8" rowspan="5"> <font color="#0f0000">122.227.189.106 [中国浙江宁波电信]</font><br /></td></tr></table>'; str+='<p><img alt="" src="https://img.jbzj.com/file_images/article/202004/2020041112113912.png" /></p>'; str+='<p>选择安装然后选择全新的sql server独立安装</p>'; str+='<p><img alt="" src="https://img.jbzj.com/file_images/article/202004/2020041112113913.png" /></p>'; console.log(str); //去除table的宽度 str=str.replace(/<table[^>]*>/ig, "<table>"); str=str.replace(/<table[^>]*>[\s\S]*?<\/table>/gi,function(match,capture){ match=match.replace(/(style|class|id)[\s]*=[\s]*("|')[^<>"']*?\2/gi,''); match=match.replace(/<\/?span[^<>]*>/gi,''); match=match.replace(/<p [^<>]*>|<\/p>/gi,''); match=match.replace(/<font[^<>]*>|<\/font>/gi,''); return match; }); //alert(str); console.log(str); </script>
本来的代码是这样的
//普通替换 function doRepNormal(s){ var str=s.replace(/<p><br type="_moz">\s*?<\/p>/ig,""); str=str.replace(/<p>\s*?<br type="_moz">\s*?<\/p>/ig, ""); str=str.replace(/<p>(\s|\ \;| | |\xc2\xa0)*?<\/p>/ig, ""); str=str.replace(/<p>\s*?<\/p>/ig,""); str=str.replace(/<p> <\/p>/ig,""); str=str.replace(/<br type="_moz">\n <\/p>/ig, "</p>"); str=str.replace(/<br type="_moz">\s*?<\/p>/ig, "</p>"); str=str.replace(/<br\s*?\/?>\s*?<\/p>/ig, "</p>"); str=str.replace(/<br \/>\n <\/p>/ig, "</p>"); str=str.replace(/<br>\n <\/p>/ig, "</p>"); //表格替换 str=str.replace(/<table[^>]*>/ig, "<table>"); str=str.replace(/<table[^>]*>[\s\S]*?<\/table>/gi,function(match,capture){ match=match.replace(/style[\s]*=[\s]*("|')[^<>"']*?\1/gi,''); match=match.replace(/cl\ass[\s]*=[\s]*("|')[^<>"']*?\1/gi,''); match=match.replace(/id[\s]*=[\s]*("|')[^<>"']*?\1/gi,''); match=match.replace(/<font[^<>]*>|<\/font>/gi,''); return match; }); str=str.replace(/<td[^>]*>/gi,function(match,capture){ match=match.replace(/style[\s]*=[\s]*("|')[^<>"']*?\1/gi,''); match=match.replace(/cl\ass[\s]*=[\s]*("|')[^<>"']*?\1/gi,''); match=match.replace(/id[\s]*=[\s]*("|')[^<>"']*?\1/gi,''); return match; }); return str; }
靠山修改成这样的
str=str.replace(/<table[^>]*>/ig, "<table>"); str=str.replace(/<table[^>]*>[\s\S]*?<\/table>/gi,function(match,capture){ match=match.replace(/(style|class|id)[\s]*=[\s]*("|')[^<>"']*?\2/gi,''); match=match.replace(/<\/?span[^<>]*>/gi,''); match=match.replace(/<p [^<>]*>|<\/p>/gi,''); match=match.replace(/<font[^<>]*>|<\/font>/gi,''); return match; });
颠末测试发明chrome中正常,但在ie8与ie7中有bug无法实现class与id的替换,文档声明<!DOCTYPE html>城市影响一些功效,真是涨了见地
正悦目到此外人分享的先留存一份,后续更新
用正则表达式取出table中的所有行(支持嵌套table)
此是通过csdn询问得高人之手写的。
感谢gzdiablo
表达式: