加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式,去除所有HTML标签

发布时间:2020-12-14 02:23:27 所属栏目:百科 来源:网络整理
导读:protected string str = "tabletrtdsdasasdsdd/td/tr/tablebrpsds/pimg id='img1' src='http://www.baidu.com/img/baidu_logo.gif' width='100' height='50' alt=''aaassssbrimg src='http://www.baidu.com/img/baidu_logo.gif' width='100' height='50' alt

protected string str = "<table><tr><td>sdasasdsdd</td></tr></table><br><p>sds</p><img id='img1' src='http://www.baidu.com/img/baidu_logo.gif' width='100' height='50' alt=''>aaassss<br><img src='http://www.baidu.com/img/baidu_logo.gif' width='100' height='50' alt=''> 说是道 ";

    protected void Page_Load(object sender,EventArgs e)
    {

        //string regexstr = @"<[^>]*>";    //去除所有的标签

        //@"<script[^>]*?>.*?</script>" //去除所有脚本,中间部分也删除
        
        // string regexstr = @"<img[^>]*>";   //去除图片的正则

       // string regexstr = @"<(?!br).*?>";   //去除所有标签,只剩br

        // string regexstr = @"<table[^>]*?>.*?</table>";   //去除table里面的所有内容

        string regexstr = @"<(?!img|br|p|/p).*?>";   //去除所有标签,只剩img,br,p
  
        str = Regex.Replace(str,regexstr,string.Empty,RegexOptions.IgnoreCase);

    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读