您现在的位置是:网站首页> 编程资料编程资料

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

2023-05-25 401人已围观

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

完整测试代码

原来的代码是这样的

 //普通替换 function doRepNormal(s){ var str=s.replace(/


\s*?<\/p>/ig,""); str=str.replace(/

\s*?
\s*?<\/p>/ig, ""); str=str.replace(/

(\s|\ \;| | |\xc2\xa0)*?<\/p>/ig, ""); str=str.replace(/

\s*?<\/p>/ig,""); str=str.replace(/

 <\/p>/ig,""); str=str.replace(/
\n <\/p>/ig, "

"); str=str.replace(/
\s*?<\/p>/ig, "

"); str=str.replace(/\s*?<\/p>/ig, "

"); str=str.replace(/
\n <\/p>/ig, "

"); str=str.replace(/
\n <\/p>/ig, "

"); //表格替换 str=str.replace(/]*>/ig, ""); str=str.replace(/]*>[\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>/gi,''); return match; }); str=str.replace(/]*>/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(/]*>/ig, "
"); str=str.replace(/]*>[\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>/gi,''); match=match.replace(/]*>|<\/font>/gi,''); return match; });

经过测试发现chrome中正常,但在ie8与ie7中有bug无法实现class与id的替换,文档声明都会影响一些结果,真是涨了见识

正好看到别的人分享的先留存一份,后续更新

用正则表达式取出table中的所有行(支持嵌套table)

此是通过csdn询问得高人之手写的。
谢谢gzdiablo

表达式:

]*>(?:(?:\s|\S)*?(?=)(?(]*>(?:\s|\S)*?(?:

|(?:(?:]*>(?:\s|\S)*?(?:\s|\S)*?)*?))(?:\s|\S)*?|))*

一条表达式就可以获取你想要的
写得好辛苦
测试:

 
 
 
 
  
 
 
  

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

============================================match1
 
     
   


     
       
       

   

 

     
       
         
       
     
 

     
 
==============================================match2
 
     
     
   

     


       
       
   
 

 
==============================================match3
 
   
     
       
       

   

 

     
     
 

正则表达式匹配html标签table

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

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

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

[\s\S]*<\/table>

[\s\S]*?<\/table>

以上两个表达式,一个加了"?"和一个却不加“?”,那么这有什么区别呢?
我们知道“?”在正则表达式里是一个通配符:匹配前面的子表达式零次或一次,或指明一个非贪婪限定符。

在这里,通过测试,我们得出这样的结论:在不加“?”的情况下,在匹配下面一段内容的时候:

这是第一个table

我不是table里的内容
这是第二个table

我也不是table里的内容
这是第三个table

会把以

开始,
结束的内容都匹配出来

加了“?”之后,只匹配第n个匹配的内容

完整测试代码小编提供

效果图

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

-六神源码网