
js中简化html字符串的table结构
当使用html()函数获取table元素时,它会包含原始的属性、样式和空格。要将其简化为仅保留基本结构,可以使用以下步骤:
- 使用replace()方法去除所有空格字符:str = str.replace(/s/g, "");
- 使用正则表达式匹配table、tr、th和td标签,并将其替换为基本结构:str.replace(/(<table|<tr|<th|<td).*?(>)/g, $1$2);
例如,对于给定的html:
<table class="..." style="..." id="...">
<tr style="..." role="row" rowspan="1" colspan="rowspan">
<th></th>
</tr>
<tr style="..." role="row" rowspan="1" colspan="rowspan">
<td></td>
</tr>
</table>简化后得到:
立即学习“前端免费学习笔记(深入)”;
<table>
<tr>
<th></th>
</tr>
<tr>
<td></td>
</tr>
</table>











