我有这样的html:
<table class="tbNoBorder" ..some attr here...>
<tr><td>1</td></tr><tr><td>2</td></tr>
</table>
<table ..some attr here... >
<tr><td>1</td></tr><tr><td>2</td></tr>
</table>
我需要使用正则表达式转换为
<table class="tbNoBorder" cellspacing="0" cellpadding="0" ..some attr here...>
<tr><td style="padding: 5px;">1</td></tr><tr><td style="padding: 5px;">2</td></tr>
</table>
<table cellspacing="0" cellpadding="0" ..some attr here... >
<tr><td style="border: solid 1px #ccc; padding: 5px;">1</td></tr><tr><td style="border: solid 1px #ccc; margin: 0; padding: 5px;">2</td></tr>
</table>
然后我将其转换为 Word,这就是我需要这种转换的原因。具有类tbNoBorder的表不能有任何边框。
我编写了这段代码来做到这一点,但所有表格都带有边框。第一个正则表达式占用所有表。有什么想法让它发挥作用吗?
//Fixes tables with borders
content = Regex.Replace(content,
@"<table(.*?)(?!tbNoBorder)(.*?)>(.*?)</table>",
m =>
{
var tableContent = Regex.Replace(m.Groups[3].ToString(),
@"<td",
t => "<td style=\"border: solid 1px #ccc; padding: 5px;\"", RegexOptions.IgnoreCase
);
return "<table cellspacing=\"0\" cellpadding=\"0\"" + m.Groups[1] + m.Groups[2] + ">" + tableContent + "</table>";
}, RegexOptions.IgnoreCase
);
//Fixes tables without borders, has class tbNoBorder
content = Regex.Replace(content,
@"<table(.*?)tbNoBorder(.*?)>(.*?)</table>",
m =>
{
var tableContent = Regex.Replace(m.Groups[3].ToString(),
@"<td",
t => "<td style=\"padding: 5px;\"", RegexOptions.IgnoreCase
);
return "<table cellspacing=\"0\" cellpadding=\"0\" + m.Groups[1] + m.Groups[2] + ">" + tableContent + "</table>";
}, RegexOptions.IgnoreCase
);