正则表达式,替换所有HTML标签

我自己写了一个正则表达式,<(.|\n)+?>
这个是替换所以HTML标签,非贪婪的,多行的。

 

 如果我想替换得到所以非HTML标签,
             我的代码就只能是这样,先找打HTML标签,然后将标签替换掉。

 

 能不能直接找到非HTML标签呢。。

 

还有个问题就是,,截取字符串的长度。 
         我下面的这种方法,没有判断中文或者非中文,截取的长度总是有长有短。 
             不知道有没有好点的办法让截取的长度,一样长的,而不是str.Length的长度。 

public static string formatString(string str, int size)
{
string temp = str;
//Encoding.Default.GetBytes(str).Length;
Regex regex = new Regex("<.+?>");
temp
= regex.Replace(str, "");
//for (int i = 0; i < regex.Matches(str).Count; i++)
//{
// temp = temp.Replace(regex.Matches(str)[i].Value, "");
//}
temp = temp.Replace("\r\n", "");
temp
= temp.Replace("&nbsp;", "");
if (temp.Length >= size)
{
temp
= temp.Substring(0, size - 3) + "";
}
return temp;
}
posted @ 2010-01-29 17:42  小师傅  阅读(511)  评论(0编辑  收藏  举报