首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

用正则表达式怎么替换非<br>的其它HTML标记

2012-03-17 
用正则表达式如何替换非br的其它HTML标记?如题[解决办法]//先把 br 替换成不会出现的字符//在把 *

用正则表达式如何替换非<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);

热点排行