精美而实用的网站,关注web编程技术、网站运营、SEO推广,让您轻松愉快的学习

项目中有时候我们需要去掉内容的一些指定HTML标签,如去除div标签,去除A标签,去除Table标签等等,下面青岛星网跟大家分享一个封装的正则函数。

ASP去除HTML标签的正则函数

<%   
  '/*   函数名称:qdxw_ReplaceHtml ClearHtml   
  '/*   函数语言:VBScript   Language     
  '/*   作  用:清除文件HTML格式函数   
  '/*   传递参数:Content   (注:需要进行清除的内容)   
  '/*   函数说明:正则匹配(正则表达式)模式进行数据匹配替换   
     
  Function   ClearHtml(Content)   
  Content=qdxw_ReplaceHtml("&#[^>]*;",   "",   Content)   
  Content=qdxw_ReplaceHtml("</?marquee[^>]*>",   "",   Content)   
  Content=qdxw_ReplaceHtml("</?object[^>]*>",   "",   Content)   
  Content=qdxw_ReplaceHtml("</?param[^>]*>",   "",   Content)   
  Content=qdxw_ReplaceHtml("</?embed[^>]*>",   "",   Content)   
  Content=qdxw_ReplaceHtml("</?table[^>]*>",   "",   Content)   
  Content=qdxw_ReplaceHtml(" ","",Content)   
  Content=qdxw_ReplaceHtml("</?tr[^>]*>",   "",   Content)   
  Content=qdxw_ReplaceHtml("</?th[^>]*>","",Content)   
  Content=qdxw_ReplaceHtml("</?p[^>]*>","",Content)   
  Content=qdxw_ReplaceHtml("</?a[^>]*>","",Content)   
  Content=qdxw_ReplaceHtml("</?img[^>]*>","",Content)   
  Content=qdxw_ReplaceHtml("</?tbody[^>]*>","",Content)   
  Content=qdxw_ReplaceHtml("</?li[^>]*>","",Content)   
  Content=qdxw_ReplaceHtml("</?span[^>]*>","",Content)   
  Content=qdxw_ReplaceHtml("</?div[^>]*>","",Content)   
  Content=qdxw_ReplaceHtml("</?th[^>]*>",   "",   Content)   
  Content=qdxw_ReplaceHtml("</?td[^>]*>",   "",   Content)   
  Content=qdxw_ReplaceHtml("</?script[^>]*>",   "",   Content)   
  Content=qdxw_ReplaceHtml("(javascript|jscript|vbscript|vbs):",   "",   Content)   
  Content=qdxw_ReplaceHtml("on(mouse|exit|error|click|key)",   "",   Content)   
  Content=qdxw_ReplaceHtml("<//?xml[^>]*>",   "",   Content)   
  Content=qdxw_ReplaceHtml("<//?[a-z]+:[^>]*>",   "",   Content)   
  Content=qdxw_ReplaceHtml("</?font[^>]*>",   "",   Content)   
  Content=qdxw_ReplaceHtml("</?b[^>]*>","",Content)   
  Content=qdxw_ReplaceHtml("</?u[^>]*>","",Content)   
  Content=qdxw_ReplaceHtml("</?i[^>]*>","",Content)   
  Content=qdxw_ReplaceHtml("</?strong[^>]*>","",Content)   
  ClearHtml=Content   
  End   Function  
     
  Function   qdxw_ReplaceHtml(patrn,   strng,content)   
  IF   IsNull(content)   Then  
  content=""  
  End   IF   
  Set   regEx   =   New   RegExp '   建立正则表达式。   
  regEx.Pattern   =   patrn '   设置模式。   
  regEx.IgnoreCase   =   true             '   设置忽略字符大小写。   
  regEx.Global   =   True '   设置全局可用性。   
  qdxw_ReplaceHtml=regEx.Replace(content,strng) '   执行正则匹配   
  End   Function  
  %>

调用函数:ClearHtml() 即可  
使用方法为:ClearHtml(Content),其中Content为欲清除的代码存放的变量

Tags:ASP 正则 HTML