Use c # To filter HTML code

Source: Internet
Author: User

// Filter HTML code
Public string checkStr (string html)
{
System. text. regularExpressions. regex regex1 = new System. text. regularExpressions. regex (@ "<script [sS] + </script *>", System. text. regularExpressions. regexOptions. ignoreCase );
System. text. regularExpressions. regex regex2 = new System. text. regularExpressions. regex (@ "href * = * [sS] * script *:", System. text. regularExpressions. regexOptions. ignoreCase );
System. Text. RegularExpressions. Regex regex3 = new System. Text. RegularExpressions. Regex (@ "on [sS] * =", System. Text. RegularExpressions. RegexOptions. IgnoreCase );
System. text. regularExpressions. regex regex4 = new System. text. regularExpressions. regex (@ "<iframe [sS] + </iframe *>", System. text. regularExpressions. regexOptions. ignoreCase );
System. text. regularExpressions. regex regex5 = new System. text. regularExpressions. regex (@ "<frameset [sS] + </frameset *>", System. text. regularExpressions. regexOptions. ignoreCase );
System. text. regularExpressions. regex regex6 = new System. text. regularExpressions. regex (@ "] +>", System. text. regularExpressions. regexOptions. ignoreCase );
System. Text. RegularExpressions. Regex regex7 = new System. Text. RegularExpressions. Regex (@ "</p>", System. Text. RegularExpressions. RegexOptions. IgnoreCase );
System. Text. RegularExpressions. Regex regex8 = new System. Text. RegularExpressions. Regex (@ "<p>", System. Text. RegularExpressions. RegexOptions. IgnoreCase );

Html = regex1.Replace (html, ""); // filter <script> </script> tags
Html = regex2.Replace (html, ""); // filter href = javascript: (<A>) attributes
Html = regex3.Replace (html, "_ disibledevent ="); // filter the on events of other controls
Html = regex4.Replace (html, ""); // filter iframe
Html = regex5.Replace (html, ""); // filter frameset
Html = regex6.Replace (html, ""); // filter frameset
Html = regex7.Replace (html, ""); // filter frameset

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.