下面是两个过滤的方法

/// <summary>
/// 此处过滤危险HTML方法
/// </summary>
/// <param name="html">html</param>
/// <returns></returns>
private string FilterHTML(string html)
{
if (html == null)
return ""; //过滤 script
Regex regex_script1 = new Regex("(<script[//s//S]*?///script//s*>)", RegexOptions.IgnoreCase);
Regex regex_script2 = new Regex("(<(script[//s//S]*?)>)", RegexOptions.IgnoreCase);
html = regex_script1.Replace(html, "");
html = regex_script1.Replace(html, ""); //过滤 <iframe> 标签
Regex regex_iframe1 = new Regex("(<iframe [//s//S]+<iframe//s*>)", RegexOptions.IgnoreCase);
Regex regex_iframe2 = new Regex("(<(iframe [//s//S]*?)>)", RegexOptions.IgnoreCase);
html = regex_iframe1.Replace(html, "");
html = regex_iframe2.Replace(html, ""); //过滤 <frameset> 标签
Regex regex_frameset1 = new Regex("(<frameset [//s//S]+<frameset //s*>)", RegexOptions.IgnoreCase);
Regex regex_frameset2 = new Regex("(<(frameset [//s//S]*?)>)", RegexOptions.IgnoreCase);
html = regex_frameset1.Replace(html, "");
html = regex_frameset2.Replace(html, ""); //过滤 <frame> 标签
Regex regex_frame1 = new Regex("(<frame[//s//S]+<frame //s*>)", RegexOptions.IgnoreCase);
Regex regex_frame2 = new Regex("(<(frame[//s//S]*?)>)", RegexOptions.IgnoreCase);
html = regex_frame1.Replace(html, "");
html = regex_frame2.Replace(html, ""); //过滤 <form> 标签
Regex regex_form1 = new Regex("(<(form [//s//S]*?)>)", RegexOptions.IgnoreCase);
Regex regex_form2 = new Regex("(<(/form[//s//S]*?)>)", RegexOptions.IgnoreCase);
html = regex_form1.Replace(html, "");
html = regex_form2.Replace(html, ""); //过滤 on: 的事件
//过滤on 带单引号的 过滤on 带双引号的 过滤on 不带有引号的
string regOn = @"<[//s//S]+ (on)[a-zA-Z]{4,20} *= *[//S ]{3,}>";
string regOn2 = @"((on)[a-zA-Z]{4,20} *= *'[^']{3,}')|((on)[a-zA-Z]{4,20} *= */""[^/""]{3,}/"")|((on)[a-zA-Z]{4,20} *= *[^>/ ]{3,})";
html = GetReplace(html, regOn, regOn2, ""); //过滤 javascript: 的事件
regOn = @"<[//s//S]+ (href|src|background|url|dynsrc|expression|codebase) *= *[ /""/']? *(javascript:)[//S]{1,}>";
regOn2 = @"(' *(javascript|vbscript):([//S^'])*')|(/"" *(javascript|vbscript):[//S^/""]*/"")|([^=]*(javascript|vbscript):[^/> ]*)";
html = GetReplace(html, regOn, regOn2, ""); return html;
} /// <summary>
/// 正则双重过滤
/// </summary>
/// <param name="content"></param>
/// <param name="splitKey1"></param>
/// <param name="splitKey2"></param>
/// <param name="newChars"></param>
/// <returns></returns>
private string GetReplace(string content, string splitKey1, string splitKey2, string newChars)
{
//splitKey1 第一个正则式匹配 //splitKey2 匹配结果中再次匹配进行替换 if (splitKey1 != null && splitKey1 != "" && splitKey2 != null && splitKey2 != "")
{
Regex rg = new Regex(splitKey1);
System.Text.RegularExpressions.MatchCollection mc = rg.Matches(content); foreach (System.Text.RegularExpressions.Match mc1 in mc)
{
string oldChar = mc1.ToString();
string newChar = new Regex(splitKey2, RegexOptions.IgnoreCase).Replace(oldChar, newChars);
content = content.Replace(oldChar, newChar);
}
return content;
}
else
{
if (splitKey2 != null && splitKey2 != "")
{
Regex rg = new Regex(splitKey2, RegexOptions.IgnoreCase);
return rg.Replace(content, newChars);
}
}
return content;
}

使用的时候

this.content.InnerHtml = FilterHTML(studentQuestionInfo.Description);

C# 正则表达式过滤危险HTML的更多相关文章

  1. PHP过滤指定字符串,过滤危险字符

    安全过滤函数,用于过滤危险字符 function safe_replace($string) {  $string = str_replace(' ','',$string);  $string = ...

  2. 【转载】C#防SQL注入过滤危险字符信息

    不过是java开发还是C#开发或者PHP的开发中,都需要关注SQL注入攻击的安全性问题,为了保证客户端提交过来的数据不会产生SQL注入的风险,我们需要对接收的数据进行危险字符过滤来防范SQL注入攻击的 ...

  3. Java正则表达式过滤出字母、数字和中文

    原文:http://blog.csdn.net/k21325/article/details/54090066 1.Java中过滤出字母.数字和中文的正则表达式 (1)过滤出字母的正则表达式 [^(A ...

  4. java 使用正则表达式过滤HTML中标签

    /** * 去掉文本中的html标签 * * @param inputString * @return */ public static String html2Text(String inputSt ...

  5. java正则表达式过滤html标签

    import java.util.regex.Matcher; import java.util.regex.Pattern; /** * <p> * Title: HTML相关的正则表达 ...

  6. asp.net正则表达式过滤标签和数据提取

    无论什么语言,正则表达式的处理方法都是非常灵活.高效的,尤其是对某些字符串的抓取.过滤方面,更显其优势. 正则表达式的写法通常比较简单,几行短代码便能轻松完成看似很复杂的事情,更值得称赞的是,它的执行 ...

  7. 正则表达式过滤联系方式,微信手机号QQ等

    有些输入不允许用户输入联系方式.可以使用以下正则表达式来判断是否输入敏感信息 var reg = new RegExp("(微信|QQ|qq|weixin|1[0-9]{10}|[a-zA- ...

  8. MySQL手工注入进阶篇——突破过滤危险字符问题

    当我们在进行手工注入时,有时候会发现咱们构造的危险字符被过滤了,接下来,我就教大家如何解决这个问题.下面是我的实战过程.这里使用的是墨者学院的在线靶场.咱们直接开始. 第一步,判断注入点. 通过测试发 ...

  9. MYSQL使用正则表达式过滤数据

    一.正则与LIKE的区别 Mysql的正则表达式仅仅使SQL语言的一个子集,可以匹配基本的字符.字符串.例如:select * from wp_posts where post_name REGEXP ...

随机推荐

  1. QMUI UI库 控件 弹窗 列表 工具类 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  2. pair练习

    /* 编写程序读入一些列string和int型数据,将每一组存储在一个pair对象中, 然后将这些pair对象存储在vector容器里. */ #include <iostream> #i ...

  3. SSE,MSE,RMSE,R-square指标讲解

    SSE(和方差.误差平方和):The sum of squares due to errorMSE(均方差.方差):Mean squared errorRMSE(均方根.标准差):Root mean ...

  4. ACM~排列组合&amp;&amp;hdu例子

    排列组合是数学中的一个分支.在计算机编程方面也有非常多的应用,主要有排列公式和组合公式.错排公式.母函数.Catalan Number(卡特兰数)等. 一.有关组合数学的公式 1.排列公式   P(n ...

  5. [PureScript] Introduce to PureScript Specify Function Arguments

    JavaScript does its error-checking at runtime, but PureScript has a compiler, which makes sure that ...

  6. 【Python】使用hashlib进行MD5和sha1摘要计算

    代码: import hashlib hash = hashlib.md5() hash.update('http://www.cnblogs.com/xiandedanteng'.encode('u ...

  7. (转)C/C++ 程序设计员应聘常见 面试笔试 试题深入剖析

    C/C++ 程序设计员应聘常见 面试笔试 试题深入剖析 http://www.nowcoder.com/discuss/1826?type=2&order=0&pos=23&p ...

  8. Android 获得图片并解码成缩略图以减少内存消耗

    本文内容 环境 演示 下载 Demo 环境 Windows 2008 R2 64 位 Eclipse ADT V22.6.2,Android 4.4.3 SAMSUNG GT-I9008L,Andro ...

  9. Solidworks如何替换工程图参考零件

    不要在左侧树形图右击修改   而是要在右侧主视图上右击,替换模型   左侧浏览找到新的零件,然后打开   替换完成之后,会有一些尺寸变成黄色,只需要改动黄色部分即可,不需要每个尺寸重新标注    

  10. android中Snackbar(Design Support)的使用

    Snackbar是Android Design Support Library中的一个组件,想使用Snackbar,必须先引入Design Support,我这里引入的是当前的最新版本: implem ...