日期:2014-05-20  浏览次数:20781 次

用正则表达式如何替换非<br>的其它HTML标记?
如题

------解决方案--------------------
//先把 <br> 替换成不会出现的字符
//在把 <*> 替换为空
//把 <br> 替换回来
//只能说先解决问题,但不是最好的方法,就当顶了

string S = @ "1 <br> 2 <b> 3 </b> 4 <br> 5 ";
S = Regex.Replace(S, " <br> ", "\0x07\0x08 ", RegexOptions.IgnoreCase);
S = Regex.Replace(S, @ " <[^> ]*> ", " ", RegexOptions.IgnoreCase);
S = Regex.Replace(S, "\0x07\0x08 ", " <br> ", RegexOptions.IgnoreCase);
------解决方案--------------------
string yourStr = ..............;
string resultStr = Regex.Replace(yourStr, @ " <(?!br> )[^> ]*?> ", " " , RegexOptions.IgnoreCase);

------解决方案--------------------
string body = Regex.Replace(lst[i].HTMLBody, @ " <br.*?> ", "\n ", RegexOptions.IgnoreCase);
body = Regex.Replace(body, @ " <[\s\S]*?> ", " ", RegexOptions.IgnoreCase);