日期:2014-05-17  浏览次数:21015 次

(string Htmlstring)能否保留<a> <b> <p> <strong> <img> <script src="/*.js"></script>

能否保留<a> <b> <p> <strong> <img> <script src="/*.js"></script>这几个标签呢 


public static string NoHTML(string Htmlstring)
    {
   
              //删除脚本   
 
             Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
 
             //删除HTML   
            // Htmlstring.Replace("</p>", "</p>\r\n");
   Htmlstring = Regex.Replace(Htmlstring, @"</p>", "</p>:hn", RegexOptions.IgnoreCase);
    // Htmlstring = Regex.Replace(Htmlstring, @"<div>", "", RegexOptions.IgnoreCase);
// Htmlstring = Regex.Replace(Htmlstring, @"</div>", "", RegexOptions.IgnoreCase);
   Htmlstring = Regex.Replace(Htmlstring, @"<(meta)[^>]*>", "", RegexOptions.IgnoreCase);

Htmlstring.Replace("<div>", "");
Htmlstring.Replace("</div>", "");
              Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
   
          //   Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "<br>", RegexOptions.IgnoreCase);
 
             Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
 
             Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);

             Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
 
             Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
 
             
 
             Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
 
             Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
 
             Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
 
             Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);