需求如下内容发布系统
编辑内容的时候会含有HTML但我现在要在前台展示的时候,
需要展示标题和一小部分内容如果只是单单的截取,那么会破坏到HTML现在要把HTML去掉,只申请文字,在截取求去掉HTML的正则表达式

解决方案 »

  1.   

            /// <summary>
            /// 移除Html标记
            /// </summary>
            /// <param name="content"></param>
            /// <returns></returns>
            public static string RemoveHtml(string content)
            {
                return Regex.Replace(content, @"<[^>]*>", string.Empty, RegexOptions.IgnoreCase);
            }
            /// <summary>
            /// 从HTML中获取文本,保留br,p,img
            /// </summary>
            /// <param name="HTML"></param>
            /// <returns></returns>
            public static string GetTextFromHTML(string HTML)
            {
                System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(@"</?(?!br|/?p|img)[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);            return regEx.Replace(HTML, "");
            }
      

  2.   

    /// <summary>
        /// 去掉html标记
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        protected static string ConvertGettext(string str)
        {
            Regex regex = new Regex(@"\<(.*?)\>", RegexOptions.IgnoreCase);        return regex.Replace(str, "").Replace("&nbsp;", "").Replace("\n", "").Replace("\r", "");
        }
      

  3.   

    不是很明白你的意思
    去掉html
    string html="你的html";
    html=Regex.Replace(html,@"(?is)<(\w+)>.*?</\1>","")
      

  4.   

    html=Regex.Replace(html,@"(?is)\<(.*?)/?\>","")