正則表達式去除所有HTML標(biāo)簽

字號:


     protected string str = "<table><tr><td>sdasasdsdd</td></tr></table><br><p>sds</p><img id='img1' src='http://pic02.newdu.com/uploads/202504/02/baidu_logo3808.gif' width='100' height='50' alt=''>aaassss<br><img src='http://pic02.newdu.com/uploads/202504/02/baidu_logo3808.gif' width='100' height='50' alt=''> 說是道 ";
        protected void Page_Load(object sender, EventArgs e)
        {
            //string regexstr = @"<[^>]*>";    //去除所有的標(biāo)簽
            //@"<script[^>]*?>.*?</script >" //去除所有腳本,中間部分也刪除
             
            // string regexstr = @"<img[^>]*>";   //去除圖片的正則
           // string regexstr = @"<(?!br).*?>";   //去除所有標(biāo)簽,只剩br
            // string regexstr = @"<table[^>]*?>.*?</table>";   //去除table里面的所有內(nèi)容
            string regexstr = @"<(?!img|br|p|/p).*?>";   //去除所有標(biāo)簽,只剩img,br,p
       
            str = Regex.Replace(str, regexstr, string.Empty, RegexOptions.IgnoreCase);
        }