欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-25 21:00:32
<% '字段内网址加上联接。 function tolink(str)     dim re '...
<%
'字段内网址加上联接。
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

%>