完整测试代码
var str='
';
str='
'; str+=' | '; str+=' | GET PING Trace Dig |
str+='
jb51.net
';str+='
';str+='
'; str+=' | |||||||||||||||
str+='
GET PING Trace Dig';str+='
jb51.net
';str+='
122.227.189.106 [中国浙江宁波电信]';str+='
';str+='
选择安装然后选择全新的sql server独立安装
';str+='
';console.log(str);
//去除table的宽度
str=str.replace(/
str=str.replace(/
match=match.replace(/(style|class|id)[\s]*=[\s]*("|')[^<>"']*?\2/gi,'');
match=match.replace(/]*>/gi,'');
match=match.replace(/
]*>|/gi,'');
match=match.replace(/]*>|/gi,'');
return match;
});
//alert(str);
console.log(str);
原来的代码是这样的
//普通替换
function doRepNormal(s){
var str=s.replace(/
\s*?/ig,"");
str=str.replace(/
\s*?
\s*?/ig, "");
str=str.replace(/
(\s|\ \;| | |\xc2\xa0)*?/ig, "");
str=str.replace(/
\s*?/ig,"");
str=str.replace(/
/ig,"");
str=str.replace(/
\n /ig, "
str=str.replace(/
\s*?/ig, "
str=str.replace(/
\s*?/ig, "
str=str.replace(/
\n /ig, "
str=str.replace(/
\n /ig, "
//表格替换
str=str.replace(/
str=str.replace(/
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(/]*>|/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(/
str=str.replace(/
match=match.replace(/(style|class|id)[\s]*=[\s]*("|')[^<>"']*?\2/gi,'');
match=match.replace(/]*>/gi,'');
match=match.replace(/
]*>|/gi,'');
match=match.replace(/]*>|/gi,'');
return match;
});
经过测试发现chrome中正常,但在ie8与ie7中有bug无法实现class与id的替换,文档声明都会影响一些结果,真是涨了见识
正好看到别的人分享的先留存一份,后续更新
用正则表达式取出table中的所有行(支持嵌套table)
此是通过csdn询问得高人之手写的。
谢谢gzdiablo
表达式:
]*>(?:(?:\s|\S)*?(?=|(?:(?:(?:\s|\S)*?)*?))(?:\s|\S)*?|))*一条表达式就可以获取你想要的
写得好辛苦
测试:
| ||||
| ||||
|
-------------------获取3个match
============================================match1
==============================================match2
==============================================match3
正则表达式匹配html标签table
首先,要匹配任意内容“.”是不行的,因为不匹配“\n”,取不到想要的内容,所以有了如下表达式:
[\s\S]*
当然,你也可以用 “[\d\D]*”、“[\w\W]*” 来表示。
现在我们来匹配一个html标签,匹配table如下:
[\s\S]*或
[\s\S]*?以上两个表达式,一个加了"?"和一个却不加“?”,那么这有什么区别呢?
我们知道“?”在正则表达式里是一个通配符:匹配前面的子表达式零次或一次,或指明一个非贪婪限定符。
在这里,通过测试,我们得出这样的结论:在不加“?”的情况下,在匹配下面一段内容的时候:
我不是table里的内容
我也不是table里的内容
会把以
结束的内容都匹配出来加了“?”之后,只匹配第n个匹配的内容
完整测试代码脚本之家小编提供
var str='
';str+='我不是table里的内容';
str+='
';str+='我也不是table里的内容';
str+='
';var reg1 = /
[\s\S]*/gi;var res1 = str.match(reg1);
console.log(res1)
var reg2 = /
[\s\S]*?/gi;var res2 = str.match(reg2);
console.log(res2)
效果图
以上就是正则表达式替换table表格中的样式与空标记(保留rowspan与colspan)的详细内容,更多关于正则表达式替换table表格的资料请关注脚本之家其它相关文章!