javascript中使用正則表達式清理table樣式的代碼
項目中遇到這樣的需求,一大段文章正文的html代碼在手機中顯示不全,原因是由于其它有table,而table表格中的tr/td都攜帶了從word中粘貼過來的樣式,需要將這一大段的字符串中的table、tr、td中攜帶的樣式清除掉,同時還不能破壞table結(jié)構(gòu),即要保留tr中的rowspan和td中的colspan屬性。
html部分代碼如下:
<p class="MsoNormal" align="left" style="text-align:left"><span lang="EN-US"> <o:p>文字中華人民共和國文字中華人民共和國文字中華人民共和國</o:p> </span></p> <table> <tbody> <tr style="height:13.5pt"> <td width="117" style="width:88.0pt;border:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:13.5pt"><p class="MsoNormal" align="center" style="text-align:center"><span style="font-family:宋體;color:#1F497D">項目<span lang="EN-US"> <o:p></o:p> </span></span></p></td> <td width="137" style="width:103.0pt;border:solid windowtext 1.0pt;border-left:none;padding:0cm 5.4pt 0cm 5.4pt;height:13.5pt"><p class="MsoNormal" align="center" style="text-align:center"><span style="font-family:宋體;color:#1F497D">金額<span lang="EN-US"> <o:p></o:p> </span></span></p></td> <td width="153" style="width:115.0pt;border:solid windowtext 1.0pt;border-left:none;padding:0cm 5.4pt 0cm 5.4pt;height:13.5pt"><p class="MsoNormal" align="center" style="text-align:center"><span style="font-family:宋體;color:#1F497D">經(jīng)辦人<span lang="EN-US"> <o:p></o:p> </span></span></p></td> <td width="135" style="width:101.0pt;border:solid windowtext 1.0pt;border-left:none;padding:0cm 5.4pt 0cm 5.4pt;height:13.5pt"><p class="MsoNormal" align="center" style="text-align:center"><span style="font-family:宋體;color:#1F497D">是否有發(fā)票<span lang="EN-US"> <o:p></o:p> </span></span></p></td> </tr> <tr style="height:13.5pt"> <td width="117" style="width:88.0pt;border:solid windowtext 1.0pt;border-top:none;padding:0cm 5.4pt 0cm 5.4pt;height:13.5pt"><p class="MsoNormal" align="center" style="text-align:center"><span style="font-family:宋體;color:#1F497D">合計<span lang="EN-US"> <o:p></o:p> </span></span></p></td> <td colspan="3" valign="bottom" nowrap="" style="width:103.0pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:13.5pt"><p class="MsoNormal" align="center" style="text-align:center"><span lang="EN-US" style="font-size:11.0pt;font-family:宋體;color:black"> <o:p></o:p> </span></p></td> </tr> </tbody> </table> <p class="MsoNormal"><span style="font-family:宋體;color:#1F497D">文字中華人民共和國文字中華人民共和國文字中華人民共和國。</span><span lang="EN-US" style="color:#1F497D"> <o:p></o:p> </span></p>
JS腳本如下:
/*
*格式化內(nèi)容,str即是html格式的字符串
*/
function formatContent(str){
str=str.replace(/<\/?(html|head|title|meta|body)\b[^>]*>/ig,"");
str=str.replace(/<table[^>]*>/ig,"<table>");
return str;
str=str.replace(/(<tr[^>]*>)/ig, function (a, b) {
if(a.indexOf('rowspan')>-1){
a=a.replace(/([a-z]+)="([^"]+)?"/ig,function(c,d,e){
return d === 'rowspan' ? (d + '="' + e + '"') : '';
})
return a;
}else{
return '<tr>';
}
});
str=str.replace(/(<td[^>]*>)/ig, function (a, b) {
if(a.indexOf('colspan')>-1){
a=a.replace(/([a-z]+)="([^"]+)?"/ig,function(c,d,e){
return d === 'colspan' ? (d + '="' + e + '"') : '';
})
return a;
}else{
return '<td>';
}
});
return str;
}
腳本之家小編再給大家推薦一個
//表格替換 str=str.replace(/<table[^<>]*>/ig, "<table>"); str=str.replace(/<thead[^<>]*>/ig, "<thead>"); str=str.replace(/<tbody[^<>]*>/ig, "<tbody>"); str=str.replace(/<tfoot[^<>]*>/ig, "<tfoot>"); str=str.replace(/<tr[^<>]*>/ig, "<tr>"); str=str.replace(/<th [^<>]*>/ig, "<th>"); str=str.replace(/<td[^<>]*>/ig, "<td>"); str=str.replace(/<th>\s*?<p>/ig, "<th>"); str=str.replace(/<\/p>\s*?<\/th>/ig, "</th>"); str=str.replace(/<td[^<>]*>\s*?<p>/ig, "<td>"); str=str.replace(/<td>\s*?<p>/ig, "<td>"); str=str.replace(/<\/p>\s*?<\/td>/ig, "</td>");
這樣對于表格中所有出現(xiàn)的標簽都進行了替換,因為現(xiàn)在都是用css控制的,基本上不用這么多參數(shù)什么的了,除非特殊的表格
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
相關(guān)文章
JavaScript使用pop方法移除數(shù)組最后一個元素用法實例
這篇文章主要介紹了JavaScript使用pop方法移除數(shù)組最后一個元素,實例分析了javascript中pop方法的使用技巧,需要的朋友可以參考下2015-04-04
JavaScript實現(xiàn)的in_array函數(shù)
這篇文章主要介紹了JavaScript實現(xiàn)的in_array函數(shù),用于判斷一個值是否在數(shù)組中,類似PHP的in_array函數(shù),需要的朋友可以參考下2014-08-08
JavaScript實現(xiàn)網(wǎng)頁端播放攝像頭實時畫面
這篇文章主要介紹了如何利用JavaScript實現(xiàn)在網(wǎng)頁端播放局域網(wǎng)(不能上云)或是廣域網(wǎng)的攝像頭的實時畫面,文中的示例代碼講解詳細,需要的可以參考一下2022-02-02
Javascript Ajax異步讀取RSS文檔具體實現(xiàn)
這篇文章主要介紹了Javascript Ajax異步讀取RSS文檔具體實現(xiàn),有需要的朋友可以參考一下2013-12-12

