开发者

Regex: To match HTML tags that do not have a "style" attribute

How do you write a Regex that matches a td by itself, eg.

<td>

and a td with any attributes, eg.

<td colspan='2' width='300'>

but not a td that has a style attribute, eg.

<td colspan='2' style='padding:2px'>

Thanks in advance! (The question ends here)

Further info on what I am doing if anyone is interested:

I want to add a style to HTML tags if they don't already have one. This is for email, not web.

I already have this code that appends css (it works, but only 开发者_C百科works if the tag already has a style attribute with closing semicolon):

/// <param name="tag">eg. "td"</param>
/// <param name="css">eg. "font:12px sans-serif;"</param>
public static string AppendStyle(string html, string tag, string css)
{
    Regex rex = new Regex(@"<"+tag+@" ([^>]*)style=['""]([^'""]*; *)['""]([^>]*)>");
    html = rex.Replace(html, @"<" + tag + " $1style='$2"+css+"'$3>");
}


It is not something you should do with regexes. That doesn't mean it's impossible though. If you do

<\w+>|<\w+ ((?!style=).)+>

You have a similar behavior like you want it ((?!(style=).)+ means 'not style='. But I agree with Jim Mischel. It is recommended to use a real parser here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜