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

有时候我们需要去除HTML标签只要里面的文字,如截取文章的前100个汉字作为文章的导读,这个时候我们只想要最前100个文字,HTML标签都要去掉。下面青岛星网跟大家分享:ASP去除所有HTML标签函数。

只取前100个文字最简单的ASP函数left即可。但是如何去除所有的HTML标签呢?

去除全部HTML标签的ASP函数

Public Function ReplaceHTML(Textstr)
Dim Str, re
Str = Textstr
Set re = New RegExp
re.IgnoreCase = True
re.Global = True
re.Pattern = "<(.[^>]*)>"
Str = re.Replace(Str, "")
Set Re = Nothing
ReplaceHTML = Str
End Function

使用正则表达式过滤所有HTML标记加强版函数

Public Function ReplaceHTML(Textstr)
Dim sStr, regEx
sStr = Textstr
Set regEx = New RegExp
regEx.IgnoreCase = True
regEx.Global = True
regEx.Multiline = True
' regEx.Pattern = "<!--[\s\S]*?-->" '//想用就把注释去掉
' sStr = regEx.Replace(sStr, "")
regEx.Pattern = "<script[\s\S]*?</script>"
sStr = regEx.Replace(sStr, "")
regEx.Pattern = "<style[\s\S]*?</style>"
sStr = regEx.Replace(sStr, "")
regEx.Pattern = "\s[on].+?=([\""|\'])(.*?)\1"
sStr = regEx.Replace(sStr, "")
regEx.Pattern = "<(.[^>]*)>"
sStr = regEx.Replace(sStr, "")
Set regEx = Nothing
ReplaceHTML = sStr
End Function
Tags:ASP HTML标签 函数