//用正则表达式过滤脚本
public string wipeScript(string html) 
{ 
System.Text.RegularExpressions.Regex regex1 = new 

System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script 

*>",System.Text.RegularExpressions.RegexOptions.IgnoreCase); 
System.Text.RegularExpressions.Regex regex2 = new 

System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script 

*:",System.Text.RegularExpressions.RegexOptions.IgnoreCase); 
System.Text.RegularExpressions.Regex regex3 = new 

System.Text.RegularExpressions.Regex(@" 

on[\s\S]*=",System.Text.RegularExpressions.RegexOptions.IgnoreCase); 
System.Text.RegularExpressions.Regex regex4 = new 

System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe 

*>",System.Text.RegularExpressions.RegexOptions.IgnoreCase); 
System.Text.RegularExpressions.Regex regex5 = new 

System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset 

*>",System.Text.RegularExpressions.RegexOptions.IgnoreCase); 
html = regex1.Replace(html, ""); //过滤<script></script>标记 
html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性 
html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件 
html = regex4.Replace(html, ""); //过滤iframe 
html = regex5.Replace(html, ""); //过滤frameset 
return html; 
} 

//对输入的字符串是否含有<script></script>判断
public bool IsValidScript(string html) 
{ 
return Regex.IsMatch(html,@"<script[\s\S]+</script *>");
}
Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐