问题描述
更多技术文章,敬请关注http://stulife.qyun.netBruce’sTechnologyBlog-专注、.net、技术、分享publicstringcheckStr(stringhtml){System.Text.RegularExpressions.Regexregex1=newSystem.Text.RegularExpressions.Regex(@"<script[sS]+</script*>",System.Text.RegularExpressions.RegexOptions.IgnoreCase);System.Text.RegularExpressions.Regexregex2=newSystem.Text.RegularExpressions.Regex(@"href*=*[sS]*script*:",System.Text.RegularExpressions.RegexOptions.IgnoreCase);System.Text.RegularExpressions.Regexregex3=newSystem.Text.RegularExpressions.Regex(@"no[sS]*=",System.Text.RegularExpressions.RegexOptions.IgnoreCase);System.Text.RegularExpressions.Regexregex4=newSystem.Text.RegularExpressions.Regex(@"<iframe[sS]+</iframe*>",System.Text.RegularExpressions.RegexOptions.IgnoreCase);System.Text.RegularExpressions.Regexregex5=newSystem.Text.RegularExpressions.Regex(@"<frameset[sS]+</frameset*>",System.Text.RegularExpressions.RegexOptions.IgnoreCase);System.Text.RegularExpressions.Regexregex6=newSystem.Text.RegularExpressions.Regex(@"<img[^>]+>",System.Text.RegularExpressions.RegexOptions.IgnoreCase);System.Text.RegularExpressions.Regexregex7=newSystem.Text.RegularExpressions.Regex(@"</p>",System.Text.RegularExpressions.RegexOptions.IgnoreCase);System.Text.RegularExpressions.Regexregex8=newSystem.Text.RegularExpressions.Regex(@"<p>",System.Text.RegularExpressions.RegexOptions.IgnoreCase);System.Text.RegularExpressions.Regexregex9=newSystem.Text.RegularExpressions.Regex(@"<[^>]*>",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,"");//过滤iframehtml=regex5.Replace(html,"");//过滤framesethtml=regex6.Replace(html,"");//过滤framesethtml=regex7.Replace(html,"");//过滤framesethtml=regex8.Replace(html,"");//过滤framesethtml=regex9.Replace(html,"");html=html.Replace("","");html=html.Replace("</strong>","");html=html.Replace("<strong>","");returnhtml;}
解决方案
解决方案二:
up
解决方案三:
zhichi.....
解决方案四:
该回复于2009-04-30 17:31:59被版主删除
解决方案五:
stringhtml="<SPANlang=EN><FONTface="TimesNewRoman">测试文字</FONT></SPAN>";Regexreg=newRegex(@"</*[^<>]*>",RegexOptions.IgnoreCase);stringnewString=reg.Replace(html,string.Empty);
解决方案六:
publicstaticstringExecute(stringstrReplaceHtml){//删除脚本strReplaceHtml=Regex.Replace(strReplaceHtml,@"<script[^>]*?>.*?</script>","",RegexOptions.IgnoreCase);//删除HTMLstrReplaceHtml=Regex.Replace(strReplaceHtml,@"([rn])[s]+","",RegexOptions.IgnoreCase);strReplaceHtml=Regex.Replace(strReplaceHtml,@"-->","",RegexOptions.IgnoreCase);strReplaceHtml=Regex.Replace(strReplaceHtml,@"<!--.*","",RegexOptions.IgnoreCase);strReplaceHtml=Regex.Replace(strReplaceHtml,@"&(quot|#34);",""",RegexOptions.IgnoreCase);strReplaceHtml=Regex.Replace(strReplaceHtml,@"&(amp|#38);","&",RegexOptions.IgnoreCase);strReplaceHtml=Regex.Replace(strReplaceHtml,@"&(lt|#60);","<",RegexOptions.IgnoreCase);strReplaceHtml=Regex.Replace(strReplaceHtml,@"&(gt|#62);",">",RegexOptions.IgnoreCase);strReplaceHtml=Regex.Replace(strReplaceHtml,@"&(nbsp|#160);","",RegexOptions.IgnoreCase);strReplaceHtml=Regex.Replace(strReplaceHtml,@"&(iexcl|#161);","xa1",RegexOptions.IgnoreCase);strReplaceHtml=Regex.Replace(strReplaceHtml,@"&(cent|#162);","xa2",RegexOptions.IgnoreCase);strReplaceHtml=Regex.Replace(strReplaceHtml,@"&(pound|#163);","xa3",RegexOptions.IgnoreCase);strReplaceHtml=Regex.Replace(strReplaceHtml,@"&(copy|#169);","xa9",RegexOptions.IgnoreCase);strReplaceHtml=Regex.Replace(strReplaceHtml,@"&#(d+);","",RegexOptions.IgnoreCase);strReplaceHtml=Regex.Replace(strReplaceHtml,@"<(.[^>]*)>","",RegexOptions.IgnoreCase);strReplaceHtml=strReplaceHtml.Replace("rn","");returnstrReplaceHtml;}
解决方案七:
没必要那么麻烦的。直接一个正则表达式就够了。