下面是网上下载的一段去除字符串中html标记的代码
但是应用在这个字符串中却不行
<FONT color=#0000ff>qweqweqwe</FONT>123123123
得到<FONT color=#0000ff>qweqweqwe123123123这里的代码格式看起来有点费劲
大家可以在这看,我就是从这下载的
http://bbs.ntc.com.cn/csharp/csharp_35417.htm请高手帮修改下,项目紧急,没有时间让我研究正则表达式了,谢谢!!public static string StripHTML(string strHtml)
{
string [] aryReg ={
  @"<script[^>]*?>.*?</script>",   @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
  @"([\r\n])[\s]+",
  @"&(quot|#34);",
  @"&(amp|#38);",
  @"&(lt|#60);",
  @"&(gt|#62);", 
  @"&(nbsp|#160);", 
  @"&(iexcl|#161);",
  @"&(cent|#162);",
  @"&(pound|#163);",
  @"&(copy|#169);",
  @"&#(\d+);",
  @"-->",
  @"<!--.*\n"
         
  }; string [] aryRep = {
   "",
   "",
   "",
   "\"",
   "&",
   "<",
   ">",
   " ",
   "\xa1",//chr(161),
   "\xa2",//chr(162),
   "\xa3",//chr(163),
   "\xa9",//chr(169),
   "",
   "\r\n",
   ""
   }; string newReg =aryReg[0];
string strOutput=strHtml;
for(int i = 0;i<aryReg.Length;i++)
{
Regex regex = new Regex(aryReg[i],RegexOptions.IgnoreCase );
strOutput = regex.Replace(strOutput,aryRep[i]);
} strOutput.Replace("<","");
strOutput.Replace(">","");
strOutput.Replace("\r\n","");
return strOutput;
}

解决方案 »

  1.   

    要不简单点用
    public static string RemoveHtml(string content)
    {
    string newstr=FilterScript(content);
    string regexstr=@"<[^>]*>";
    return Regex.Replace(newstr,regexstr,string.Empty,RegexOptions.IgnoreCase);
    }
      

  2.   

    FilterScript是个什么函数?是楼主自己的函数吗?
      

  3.   

    楼上写成楼主了:-(,楼主的正则表达式看起来太费尽
    楼主可以在原来的基础上再去除一次
    string pattern = @"<FONT color=.*>";
    strOutput = Regex.Replace(strOutput, pattern, string.Empty);
      

  4.   

    Regex.Replace(strAll,@"(?:<script.*?>.*?</script>)|(?:<.*?>)","",RegexOptions.IgnoreCase);