下面是两个过滤的方法

/// <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);

最新文章

  1. Linux sendmail发送邮件失败诊断案例(一)
  2. INSTALL_FAILED_INSUFFICIENT_STORAGE(转发)
  3. sublime text 全局搜索
  4. ubuntu 常用命令集合版(一)【大侠勿喷,菜鸟欢迎】(转载)
  5. Linux下串口ttyS2,ttyS3不能用的问题解决办法
  6. Oracle VM VirtualBox虚拟机安装系统
  7. 监控SQL Server的job执行情况
  8. JPA @PersistenceContext和@Transactional Annotation
  9. iOS项目之iPhoneX遇到的坑
  10. 【Oracle】ORA-14400: 插入的分区关键字未映射到任何分区
  11. 编码标准:ASCII、GBK、Unicode(UTF8、UTF16、UTF32)
  12. Kafka吞吐量测试案例
  13. 洛谷P4240 毒瘤之神的考验 【莫比乌斯反演 + 分块打表】
  14. Python3 tkinter基础 LabelFrame StringVar 单击按钮,Label中显示的文字更换
  15. angular 学习日志
  16. 数据库SQL语言学习--上机练习4(视图)
  17. php抛出异常
  18. 41:和为S的两个数
  19. 解读linux中用户密码规则及忘记root口令的破解(思路)
  20. Ecmall二次开发-增删改查操作

热门文章

  1. pthread_create 报函数参数不匹配问题
  2. 初识EntityFramework6【转】
  3. ASP.NET 网站管理工具介绍
  4. 用jstl标签判断一个字符串是否包含了另一个字符串
  5. 原生javascript实现异步的7种方式
  6. 通过经纬度坐标计算距离的方法(经纬度距离计算)ZZ
  7. 使用TensorFlow动手实现一个Char-RNN
  8. linux邮件系统的优势和便利性
  9. Java中动态代理方式:
  10. Docker创建虚机和swarm