ASP为字符串中的网址自动加上链接

爱蒂网

ASP为字符串中的网址自动加上链接

2020-03-06 11:30:10 分类 / 网络编程 来源 / 互联网

asp下用正则实现字符串中的网址加链接的代码

<%
'字段内网址加上联接。
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='https://www.aidi.net.cn//article/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='https://www.aidi.net.cn//article/" + D(I) + "://$1' target=_blank>" + D(I) + "://$1</A> ")
        Next
        '***************************************************************
    End With
    Set RE = Nothing
    ToLink = strContent
End Function

%>

猜你喜欢