C# 删除html标签,只保留文字

        public static string noHtml(string str)
        {
            if (str != null)
            {
                str = Regex.Replace(str, @"<script[^>]*>[\s\S]*?</script>", "", RegexOptions.IgnoreCase);//删除脚本
                str = Regex.Replace(str, @"(<style)+[^<>]*>[^\0]*(<\/style>)+", "", RegexOptions.IgnoreCase);//删除样式
                str = Regex.Replace(str, @"<object.*?/object>", "", RegexOptions.IgnoreCase);//删除object
                str = Regex.Replace(str, @"<!--.*", "", RegexOptions.IgnoreCase);//删除开始注释
                str = Regex.Replace(str, @"-->", "", RegexOptions.IgnoreCase);//删除结尾注释
                //str = Regex.Replace(str, @"<\/*[^<>]*>", "", RegexOptions.IgnoreCase);//删除全部html
                //str = Regex.Replace(str, @"<(\/){0,1}div[^<>]*>", "", RegexOptions.IgnoreCase);//删除div
                //str = Regex.Replace(str, @"<(\/){0,1}a[^<>]*>", "", RegexOptions.IgnoreCase);//删除超链接
                //str = Regex.Replace(str, @"<(\/){0,1}font[^<>]*>", "", RegexOptions.IgnoreCase);//删除文字样式
                //str = Regex.Replace(str, @"(class=){1,}(""|\'){0,1}\S+(""|\'|>|\s){0,1}", "", RegexOptions.IgnoreCase);//删除class
                //str = Regex.Replace(str, @"(<iframe){1,}[^<>]*>[^\0]*(<\/iframe>){1,}", "", RegexOptions.IgnoreCase);//删除框架
                //str = Regex.Replace(str, @"(<script){1,}[^<>]*>[^\0]*(<\/script>){1,}", "", RegexOptions.IgnoreCase);//删除脚本
                str = Regex.Replace(str, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);//删除全部html
                str = Regex.Replace(str, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);//删除换行
                str = Regex.Replace(str, @"&nbsp;&nbsp;", " ", RegexOptions.IgnoreCase);//替换空格
            }
            return str;
        }

转载于:https://www.cnblogs.com/cive/archive/2012/10/06/2712753.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值