ASP 自动识别链接函数

找了很多地方,发现一个还不错的,不过多少有点问题,经过zx的指点,参照Z-Blog处理UBB的函数修改了一下,基本还算好用。

Function autoLink(str)
str=" "&str
Set re=new regExp     '建立正则表达式对象regular expression
re.IgnoreCase=true     '忽略大小写
re.Global=true            '搜索匹配字串针对全部文本
'查找带协议前缀的链接
re.Pattern="(http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?)"
str=re.replace(str,"<a href='$1' target='_blank'>$1</a>")
'查找不带协议前缀的链接
re.Pattern="([^(http:\/\/)])(www\.([\w-]+\.)+[\w]+(\/[\w-]+)*[\/]?([\w-]+\.[\w]+)?(\?[\w]+=[\w]+(&[\w]+=[\w]+)*)?)"
str=re.replace(str,"$1<a href='http://$2' target='_blank'>$2</a>")
'查找邮件链接
re.Pattern="(mailto:)?([\w]+@([\w-]+\.)+[\w]+)"
str=re.replace(str,"<a href='mailto:$2'>$1$2</a>")
Set re=nothing
autolink=trim(str)
End Function

主要是正则表达式的应用啊。

5 thoughts to “ASP 自动识别链接函数”

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注