您所在的位置:小祥子 » 编程 » ASP » 正文

ASP去掉字符串中的html标记

时间:2015-02-10 编辑:本站 来源:本站原创

ASP替换去掉HTML标记,但是保留换行之类的。

Function NoHtml(str)
 Set re=new RegExp
 re.IgnoreCase =true
 re.Global=True
 str = replace(str,"<br>","{br}")
 str = replace(str,"<p>","{p}")
 str = replace(str,"</p>","{/p}")
 str = replace(str,"<b>","{b}")
 str = replace(str,"</b>","{/b}")
 str = replace(str,"<strong>","{strong}")
 str = replace(str,"</strong>","{/strong}")  
 re.Pattern="(\<.[^\<]*\>)"
 str=re.replace(str,"")
 re.Pattern="(\<\/[^\<]*\>)"
 str=re.replace(str,"")
 re.Pattern="/(^[\\s]*)/g"
 str=re.replace(str,"")
 str = replace(str,"{br}","<br>")
 str = replace(str,"{p}","<p>")
 str = replace(str,"{/p}","</p>")
 str = replace(str,"{b}","<b>")
 str = replace(str,"{/b}","</b>")
 str = replace(str,"{strong}","<strong>")
 str = replace(str,"{/strong}","</strong>")  
 NoHtml=str
 Set re=Nothing
End Function

思路很简单,先把换行加粗之类的HTML标记替换成{},然后正则去掉所有的HTML标记,再又替换过来。

关键词:字符串 标记 html