用于剔除内容里 "<" ">"之间的HTML代码,以保证显示在网页中的都是实际内容,不带一点HTML代码。问题解决,结帖给分。

解决方案 »

  1.   

    试下string yourStr = ............;
    string resultStr = Regex.Replace(yourStr, @"<[\s\S]*?>","" , RegexOptions.IgnoreCase);
      

  2.   

    <.+?>可匹配所有html标签!
      

  3.   

    string str = "<a>DSFDSF<B>fasf<img>dsfaf<td>dfaf</td>";
    Response.Write(System.Text.RegularExpressions.Regex.Replace(str,"<([^>]*?)>",""));
      

  4.   

    using System;
    using System.Web;
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;
    namespace TopWin.Common
    {
        /// <summary>
        /// 处理HTML
        /// </summary>
        public class HTML
        {
            #region 去除HTML标记
            /// <summary>
            /// 去除HTML标记
            /// </summary>
            /// <param name="Htmlstring">包括HTML的源码 </param>
            /// <returns>已经去除后的文字</returns>
            public static string NoHTML(string Htmlstring)
            {
                if (Htmlstring == null)
                {
                    return "";
                }
                //删除脚本
                Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
                //删除HTML
                Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", 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, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", 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);
                Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);            Htmlstring.Replace("<", "");
                Htmlstring.Replace(">", "");
                Htmlstring.Replace("\r\n", "");
                //防止有漏掉的HTML代码
                Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();            return Htmlstring;
            }
            #endregion        /// <summary>
            /// 将C#中的回车(\r)和空格(" ")换成HTML格式对应的换行和空格
            /// </summary>
            /// <param name="strInPut"></param>
            /// <returns></returns>
            static public string Text2Html(string strInPut)
            {
                StringBuilder Temp = new StringBuilder();
                Temp.Append(strInPut);            Temp.Replace(" ", "&nbsp;");
                Temp.Replace("<", "&lt;");
                Temp.Replace(">", "&gt;");
                Temp.Replace("\r", "<br>");
                //Temp.Replace("&","&amp;");
                Temp.Replace("\"", "&quot;");
                return Temp.ToString();
            }        /// <summary>
            /// 将HTML格式的换行和空格换成C#中的对应字符("\r"、" ")
            /// </summary>
            /// <param name="strInPut"></param>
            /// <returns></returns>
            static public string Html2Text(string strInPut)
            {
                StringBuilder Temp = new StringBuilder();
                Temp.Append(strInPut);            Temp.Replace("<br>", "\r");
                Temp.Replace("&nbsp;", " ");
                Temp.Replace("&lt;", "<");
                Temp.Replace("&gt;", ">");
                //Temp.Replace("&amp;","&");
                Temp.Replace("&quot;", "\"");
                return Temp.ToString();
            }    }
    }
      

  5.   

    HTML里面的脚本、热点、注释,全部删除