C# 正则表达式 去除HTML标签 C#后台

  private static string RemoveSpecifyHtml(string ctx)
        {

            string[] holdTags = { "a", "img" };//保留的 tag  

            // <(?!((/?\s?li\b)|(/?\s?ul\b)|(/?\s?a\b)|(/?\s?img\b)|(/?\s?br\b)|(/?\s?span\b)|(/?\s?b\b)))[^>]+>  

            string regStr = string.Format(@"<(?!((/?\s?{0})))[^>]+>", string.Join(@"\b)|(/?\s?", holdTags));

            Regex reg = new Regex(regStr, RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase);
            string str = reg.Replace(ctx, "");
            Regex regex = new Regex("&.+?;", RegexOptions.IgnoreCase);

            return regex.Replace(str, "");

        }

posted on 2013-03-22 15:48  小小文豪  阅读(474)  评论(0编辑  收藏  举报

导航