服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - 正则表达式 - 正则表达式实例:截字符串及去除HTML标记

正则表达式实例:截字符串及去除HTML标记

2020-07-05 15:03正则表达式教程网 正则表达式

本文主要讲解使用正则表达式截取字符串及去除HTML标记的两种实例方法,有需要的可以参考下

  1. <% 
  2. '************************************************** 
  3. '函数名:gotTopic 
  4. '作 用:截字符串,汉字一个算两个字符,英文算一个字符 
  5. '参 数:str ----原字符串 
  6. ' strlen ----截取长度 
  7. '返回值:截取后的字符串 
  8. '************************************************** 
  9. function gotTopic(str,strlen) 
  10. if str="" then 
  11. gotTopic="" 
  12. exit function 
  13. end if 
  14. dim l,t,c, i 
  15. str=replace(replace(replace(replace(str," "," "),""",chr(34)),">",">"),"<","<") 
  16. str=replace(str,"?",""
  17. l=len(str) 
  18. t=0 
  19. for i=1 to l 
  20. c=Abs(Asc(Mid(str,i,1))) 
  21. if c>255 then 
  22. t=t+2 
  23. else 
  24. t=t+1 
  25. end if 
  26. if t>=strlen then 
  27. gotTopic=left(str,i) & "…" 
  28. exit for 
  29. else 
  30. gotTopic=str 
  31. end if 
  32. next 
  33. gotTopic=replace(replace(replace(replace(gotTopic," "," "),chr(34),"""),">",">"),"<","<") 
  34. end function 
  35. '========================================================= 
  36. '函数:RemoveHTML(strHTML) 
  37. '功能:去除HTML标记 
  38. '参数:strHTML --要去除HTML标记的字符串 
  39. '========================================================= 
  40. Function RemoveHTML(strHTML)  
  41. Dim objRegExp, Match, Matches  
  42. Set objRegExp = New Regexp  
  43. objRegExp.IgnoreCase = True  
  44. objRegExp.Global = True  
  45. '取闭合的<>  
  46. objRegExp.Pattern = "<.+?>"  
  47. '进行匹配  
  48. Set Matches = objRegExp.Execute(strHTML)  
  49. ' 遍历匹配集合,并替换掉匹配的项目  
  50. For Each Match in Matches  
  51. strHtml=Replace(strHTML,Match.Value,"")  
  52. Next  
  53. RemoveHTML=strHTML  
  54. Set objRegExp = Nothing  
  55. set Matches=nothing 
  56. End Function  
  57. %> 

延伸 · 阅读

精彩推荐