自动给文章中的邮箱和超级链接地址加上链接

Function ToLink(Str)
Dim RE '正则表达式对象
Dim strContent
If IsNull(Str) Then Str=""
Set RE = New RegExp '创建正则表达式对象
With RE
.Global = True '搜索应用于整个字符串
.IgnoreCase = True '搜索不区分大小写的
strContent=Str

'***************************************************************
'邮件地址链接自动设置
'***************************************************************
.Pattern="([\w]*)@([\w\.]*)"
strContent=.Replace(strContent,"<A Href='mailto:$1@$2'>$1@$2</A> ")

'***************************************************************
'链接自动设置
'***************************************************************
'======根据要求再添加协议名称=======
Dim D(3),I
D(0)="http"
D(1)="ftp"
D(2)="news"
D(3)="mms"
'===================================


For I=0 To UBound(D)
.Pattern=D(I) + ":\/\/([\w\.]*)"
strContent=.Replace(strContent,"<A Href='" + D(I) + "://$1' target=_blank>" + D(I) + "://$1</A> ")
Next
'***************************************************************

End With
Set RE=Nothing
ToLink=strContent
End Function

本函数不足之处:
1.对邮箱地址验证不足,如abc@abc.abc将会通过
2.对链接地址验证不足,如http://www.abc.abc将会通过
3.无法对没有http://开头的地址形如www.mzwu.com以及地址后面的参数形如?id=1&key=aaa加上链接

评论: 0 | 引用: 0 | 查看次数: 7761
发表评论
登录后再发表评论!