I have html like this:
<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>
I need to convert using Regex to
<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>
Then I convert it to Word, that is why I need this convertion. Tables which has class tbNoBorder must not have any borders.
I wrote this code to do this, but all tables comes with borders. The first Regex takes all tables. Any ideas to get it work?
//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
);