正则表达式替换table表格中的样式与空标记(保留

<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|\&nbsp\;| | |\xc2\xa0)*?<\/p>/ig, ""); str=str.replace(/<p>\s*?<\/p>/ig,""); str=str.replace(/<p>&nbsp;<\/p>/ig,""); str=str.replace(/<br type="_moz">\n&nbsp;<\/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&nbsp;<\/p>/ig, "</p>"); str=str.replace(/<br>\n&nbsp;<\/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

表达式:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwywyz.html