日期:2014-05-16  浏览次数:20399 次

在线等,一个正则表达式的问题,希望高手赐教
<table   cellspacing= " "   cellpadding= " "   width= " "   border= " ">
        <tr>
            <td> aaaaaa </td>
            <td> bbbbb </td>
        </tr>  

        ...
      </table>

我想把上面table的属性值都改成下面的样子,不知道怎么改,正则表达式
一时半会也看不的很明白,希望那位大侠用正则表达式表示出来,感激不尽,谢谢。
<table   cellspacing= "0 "   cellpadding= "0 "   width= "100% "   border= "1 ">
        <tr>
            <td> aaaaaa </td>
            <td> bbbbb </td>
        </tr>  

        ...
      </table>


------解决方案--------------------
可以试试不用正则表达式看看

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN ">
<html>
<head>
<meta http-equiv= "content-type " content= "text/html; charset=windows-1250 ">

<title> </title>
<script language=javascript>

window.onload=function(){

var t=document.getElementsByTagName( "table ");
for(var i=0;i <t.length;i++){
t[i].cellspacing=0;
t[i].cellspacing=0;
t[i].width= "100% ";
t[i].border=1;
}
}
</script>
</head>
<body>

<table cellspacing= " " cellpadding= " " width= " " border= " ">
<tr>
<td> aaaaaa </td>
<td> bbbbb </td>
</tr>


</table>

<table cellspacing= " " cellpadding= " " width= " " border= " ">
<tr>
<td> cccccccccccc </td>
<td> ddddddddddddd </td>
</tr>


</table>



</body>
</html>
------解决方案--------------------
不太清楚的的规则,是原来就有cellspacing= " "等等这些属性,然后替换为下面的形式,还是说不管原来 <table> 是否有属性,都替换为下面的形式,看看下面的是不是你要的效果吧

<script language=javascript>
function repalceTable(str) {
str=str.replace(/ <table[^> ]*> /ig, " <table cellspacing=\ "0\ " cellpadding=\ "0\ " width=\ "100%\ " border=\ "1\ "> ");
return str;
}
</script>

输入文本: <br/>
<textarea cols=80 rows=10 name=mytest>
<table cellspacing= " " cellpadding= " " width= " " border= " ">
<tr>
<td> aaaaaa </td>
<td> bbbbb </td>
</tr>

...
</table>
</textarea>
<br/>
<input type=button value= "Replace " onclick= "document.all.result.value=repalceTable(document.all.mytest.value) ">
<br/> 替换结果: <br/>
<textarea cols=80 rows=10 name=result>
</textarea&