js正則匹配table tr
我覺得比ijao簡單,但是腦子就是轉不過彎。。。。請正則高手看看阿。
<textarea id="txt">
<table border=1 width="20%">
<tr><th>title</th><th>title2</th></tr>
<tr rs="{id:\'section006\'}"><td>data1</td><td>data2 </td>
</tr>
<tr ><td>data1</td><td>data2</td></tr>
</table>
</textarea>
<script>
var str = document.getElementById('txt').value;
alert(str);
</script>
/<tr[^>]*>[\s\S]*?<\/tr>/gi,
分享一個函數
//普通替換
function doRepNormal(s){
var str=s.replace(/<p><br type="_moz">\s*?<\/p>/ig,"");
str=str.replace(/<p>\s*?<br type="_moz">\s*?<\/p>/ig, "");
str=str.replace(/<p>(\s|\ \;| | |\xc2\xa0)*?<\/p>/ig, "");
str=str.replace(/<p>\s*?<\/p>/ig,"");
str=str.replace(/<p> <\/p>/ig,"");
str=str.replace(/<br type="_moz">\n <\/p>/ig, "</p>");
str=str.replace(/<br type="_moz">\s*?<\/p>/ig, "</p>");
str=str.replace(/<br\s*?\/?>\s*?<\/p>/ig, "</p>");
str=str.replace(/<br \/>\n <\/p>/ig, "</p>");
str=str.replace(/<br>\n <\/p>/ig, "</p>");
//表格替換
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>");
return str;
}
以上就是js正則匹配table tr的詳細內容,更多關于js正則table的資料請關注腳本之家其它相關文章!
相關文章
正則表達式Regular Expression (RegExp)詳解
正則表達式,又稱正規(guī)表示式、正規(guī)表示法、正規(guī)表達式、規(guī)則表達式、常規(guī)表示法(英語:Regular Expression,在代碼中常簡寫為regex、regexp或RE2015-11-11

