asp.net去除字符串中html标签

asp.net去除字符串中html标签

具体代码如下:

  •  
  • C# 代码   复制
  • 
    using System.Text.RegularExpressions;
    
    public static string ClearHTMLTag(string strHtml)
    {
       string[] arrayReg ={
              @"<script[^>]*?>.*?</script>",
    
              @"<(\\/\\s*)?!?((\\w+:)?\\w+)(\\w+(\\s*=?\\s*(([""'])(file://[""'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[] arrayRep = {
               "",
               "",
               "",
               "\\"",
               "&",
               "<",
               ">",
               " ",
               "\\xa1",//chr(161),
               "\\xa2",//chr(162),
               "\\xa3",//chr(163),
               "\\xa9",//chr(169),
               "",
               "\\r\\n",
               ""
              };
    
       string strReturn = strHtml;
       for (int i = 0; i < arrayReg.Length; i++)
       {
          Regex regex = new Regex(arrayReg[i], RegexOptions.IgnoreCase);
          strReturn = regex.Replace(strReturn , arrayRep[i]);
       }
    
       strReturn = strReturn.Replace("<", "");
       strReturn = strReturn.Replace(">", "");
       strReturn = strReturn.Replace(\\r\\n, "");
    
    
       return strReturn;
    }