[asp学习]怎样过滤html元素

[asp学习]怎样过滤html元素,第1张

Function HTMLDecode(reString)Dim Str:Str=reStringIf Not IsNull(Str) Then Str = Replace(Str, "&", "&") Str = Replace(Str, ">", ">") Str = Replace(Str, "<", "<") Str = Replace(Str, " ", CHR(32))Str = Replace(Str, " ", CHR(9)) Str = Replace(Str, "    ", CHR(9)) Str = Replace(Str, """, CHR(34)) Str = Replace(Str, "'", CHR(39)) Str = Replace(Str, "", CHR(13)) Str = Replace(Str, "

", CHR(10)) HTMLDecode = StrEnd IfEnd Function'去掉html标签,去掉换行Function NoHtml(strText) Dim regEx Set regEx = New RegExp regEx.Global = True regEx.IgnoreCase = True regEx.Pattern = "\n" strText = regEx.Replace(strText,"") regEx.Pattern = "\r" strText = regEx.Replace(strText,"") regEx.Pattern = "(.*?)<\/scrīpt>" strText = regEx.Replace(strText,"") regEx.Pattern = "<(.+?)>" strText = regEx.Replace(strText,"")NoHtml = strTextEnd Function function strcurrency(str)str=mid(cstr(formatcurrency(str,2)),2) strcurrency=str end function

没有几种.

原理就是删掉正则匹配到的html标签

至于删除用repalce还是remove我觉得没那么重要了就..

html标签的正则,网上抄的不知道对不对:

"<(.[^>]*)>"

public static string NoHtml(string text)

{

//删除脚本

text = Regex.Replace(text, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase)

//删除HTML

text = Regex.Replace(text, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase)

text = Regex.Replace(text, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase)

text = Regex.Replace(text, @"-->", "", RegexOptions.IgnoreCase)

text = Regex.Replace(text, @"<!--.*", "", RegexOptions.IgnoreCase)

text = Regex.Replace(text, @"&(quot|#34)", "\"", RegexOptions.IgnoreCase)

text = Regex.Replace(text, @"&(amp|#38)", "&", RegexOptions.IgnoreCase)

text = Regex.Replace(text, @"&(lt|#60)", "<", RegexOptions.IgnoreCase)

text = Regex.Replace(text, @"&(gt|#62)", ">", RegexOptions.IgnoreCase)

text = Regex.Replace(text, @"&(nbsp|#160)", " ", RegexOptions.IgnoreCase)

text = Regex.Replace(text, @"&(iexcl|#161)", "\xa1", RegexOptions.IgnoreCase)

text = Regex.Replace(text, @"&(cent|#162)", "\xa2", RegexOptions.IgnoreCase)

text = Regex.Replace(text, @"&(pound|#163)", "\xa3", RegexOptions.IgnoreCase)

text = Regex.Replace(text, @"&(copy|#169)", "\xa9", RegexOptions.IgnoreCase)

text = Regex.Replace(text, @"(\d+)", "", RegexOptions.IgnoreCase)

text.Replace("<", "")

text.Replace(">", "")

text.Replace("\r\n", "")

text = HttpContext.Current.Server.HtmlEncode(text).Trim()

return text

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/6138049.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-16
下一篇 2023-03-16

发表评论

登录后才能评论

评论列表(0条)

保存