网络编程 
首页 > 网络编程 > 浏览文章

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

(编辑:jimmy 日期: 2024/11/16 浏览:3 次 )

完整测试代码

<script>
var str='<p>';
str='<table id="tblSort" cellspacing="0" helvetica="" sans=""><tr id="sdf"><td style="color:red" class="ok" colspan="8" height="500" rowspan="5">';
str+='<td style = "color:blue" style="color:blue" colspan="8" style="color:red" rowspan="5">';
str+='<td id="asdfj" class="dbclick"><span id="s159015" class="p_check"><a style="text-decoration: none; color: rgb(0,136,255)" href="http://www.17ce.com/site/dns/20200408_3875c5d0799211eab8bc2bbb7b33a12c:1.html#s159015" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >GET</a> <a style="text-decoration: none; color: rgb(0,136,255)" href="http://www.17ce.com/site/dns/20200408_3875c5d0799211eab8bc2bbb7b33a12c:1.html#s159015" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >PING</a> <a style="text-decoration: none; color: rgb(0,136,255)" href="http://www.17ce.com/site/dns/20200408_3875c5d0799211eab8bc2bbb7b33a12c:1.html#s159015" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Trace</a> <a style="text-decoration: none; color: rgb(0,136,255)" href="http://www.17ce.com/site/dns/20200408_3875c5d0799211eab8bc2bbb7b33a12c:1.html#s159015" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Dig</a></span></td>';
str+='<td class="sdf" colspan="8" style="color:red" rowspan="5"><p style="color:red">jb51.net</p></td>';
str+='<td class="sdf" colspan="8" style="color:red" rowspan="5" width="100"></td></tr></table>';

str+='<table><tr style = "color:bluefffffffffffffffffffffffffffffffffffffffffff"><td style="color:green" class="ok" colspan="8" height="500" rowspan="5">';
str+='<tr id="trinfo146798" class="alt">   <td id="tdinfo146798" colspan="16"></td>  </tr>';
str+='<td id="asdfj" class="dbclick"><span id="s159015" class="p_check"><a style="text-decoration: none; color: rgb(0,136,255)" href="http://www.17ce.com/site/dns/20200408_3875c5d0799211eab8bc2bbb7b33a12c:1.html#s159015" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >GET</a> <a style="text-decoration: none; color: rgb(0,136,255)" href="http://www.17ce.com/site/dns/20200408_3875c5d0799211eab8bc2bbb7b33a12c:1.html#s159015" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >PING</a> <a style="text-decoration: none; color: rgb(0,136,255)" href="http://www.17ce.com/site/dns/20200408_3875c5d0799211eab8bc2bbb7b33a12c:1.html#s159015" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Trace</a> <a style="text-decoration: none; color: rgb(0,136,255)" href="http://www.17ce.com/site/dns/20200408_3875c5d0799211eab8bc2bbb7b33a12c:1.html#s159015" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Dig</a></span></td>';
str+='<td class="sdf" colspan="8" style="color:red" rowspan="5"><p style="color:red">jb51.net</p></td>';
str+='<td class="sdf" colspan="8" style="color:red" rowspan="5" width="100"> <font color="#0f0000">122.227.189.106 [中国浙江宁波电信]</font><br /></td></tr></table>';

str+='<p><img alt="" src="/UploadFiles/2021-04-02/2020041112113912.png">
//普通替换
function doRepNormal(s){
var str=s.replace(/<p><br type="_moz">\s*"");
str=str.replace(/<p>\s*"_moz">\s*"");
str=str.replace(/<p>(\s|\&nbsp\;| | |\xc2\xa0)*"");
str=str.replace(/<p>\s*"");
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>");
str=str.replace(/<br\s*"</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]*"|')[^<>"']*"|')[^<>"']*"|')[^<>"']*"|')[^<>"']*"|')[^<>"']*"|')[^<>"']*"htmlcode">
str=str.replace(/<table[^>]*>/ig, "<table>");
str=str.replace(/<table[^>]*>[\s\S]*"|')[^<>"']*"htmlcode">
<table width="300" border="1" cellspacing="0" cellpadding="0">
 <tr>
 <td>&nbsp;</td>
 <td><table width="100%" border="1" cellspacing="0" cellpadding="0">
  <tr>
  <td>&nbsp;</td>
  </tr>

 </table>
  <table width="100%" border="1" cellspacing="0" cellpadding="0">
  <tr>
   <td>&nbsp;</td>
  </tr>
  </table></td>
 <td>&nbsp;</td>
 </tr>
 <tr>
 <td>&nbsp;</td>
 <td>&nbsp;</td>
 <td><table width="100%" border="1" cellspacing="0" cellpadding="0">

  <tr>
  <td>&nbsp;</td>
  </tr>
 </table></td>
 </tr>
 <tr>
 <td><table width="100%" border="1" cellspacing="0" cellpadding="0">
  <tr>
  <td>&nbsp;</td>
  </tr>

 </table></td>
 <td>&nbsp;</td>
 <td>&nbsp;</td>
 </tr>
</table>

-------------------获取3个match

============================================match1
  <tr>
    <td>&nbsp;</td>
    <td><table width="100%" border="1" cellspacing="0" cellpadding="0">
      <tr>
        <td>&nbsp;</td>
        </tr>

    </table>
      <table width="100%" border="1" cellspacing="0" cellpadding="0">
        <tr>
          <td>&nbsp;</td>
        </tr>
      </table></td>
    <td>&nbsp;</td>
  </tr>
==============================================match2
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><table width="100%" border="1" cellspacing="0" cellpadding="0">

      <tr>
        <td>&nbsp;</td>
        </tr>
    </table></td>
  </tr>
==============================================match3
  <tr>
    <td><table width="100%" border="1" cellspacing="0" cellpadding="0">
      <tr>
        <td>&nbsp;</td>
        </tr>

    </table></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>

正则表达式匹配html标签table

首先,要匹配任意内容“.”是不行的,因为不匹配“\n”,取不到想要的内容,所以有了如下表达式:
    [\s\S]*

当然,你也可以用 “[\d\D]*”、“[\w\W]*” 来表示。

现在我们来匹配一个html标签,匹配table如下:

<table.*""和一个却不加“"htmlcode">

<script>
var str='<table>这是第一个table</table>';
str+='我不是table里的内容';
str+='<table>这是第二个table</table>';
str+='我也不是table里的内容';
str+='<table>这是第三个table</table>';

var reg1 = /<table.*"" src="/UploadFiles/2021-04-02/20200411131759.png">

以上就是正则表达式替换table表格中的样式与空标记(保留rowspan与colspan)的详细内容,更多关于正则表达式替换table表格的资料请关注其它相关文章!

上一篇:js中的正则表达式入门(大量实例代码)
下一篇:55分钟学会正则表达式(来自Github)
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。