当前位置: 移动技术网 > IT编程>开发语言>正则 > asp下正则实现URL自动链接的一个函数

asp下正则实现URL自动链接的一个函数

2017年12月12日  | 移动技术网IT编程  | 我要评论

复制代码 代码如下:

function autolinkurls(strstring)  
    dim match, matches, offset, url, email, link, relnkautolinkurl  
    relnkautolinkurl = "<a href=""[[%url%]]"">[[%urltext%]]</a>"  
    if not isobject(regexp) then set regexp = new regexp  
    regexp.global = true  
    regexp.ignorecase = true  
    'look for urls  
    regexp.pattern = "(((ht|f)tps?://)|(www\.))([\w-]+\.)+[\w-:]+(/[\w- ./?%#;&=]*)?"  
    set matches = regexp.execute(strstring)  
    offset = 0  
    for each match in matches  
        url = match  
        if left(url, 4) = "www." then url = "http://" & url  
        link = replace(replace(relnkautolinkurl, "[[%urltext%]]", match), "[[%url%]]", url)  
        strstring = mid(strstring, 1, match.firstindex + offset) & link & mid(strstring, match.firstindex + 1 + match.length + offset, len(strstring))  
        offset = offset + len(link) - len(match)  
    next  
    'look for emails  
    regexp.pattern = "[a-za-z0-9_+-.']+@\w+([-.]\w+)*\.\w+([-.]\w+)*"  
    set matches = regexp.execute(strstring)  
    offset = 0  
    for each match in matches  
        email = match  
        link = replace(replace(relnkautolinkurl, "[[%urltext%]]", match), "[[%url%]]", "mailto:" & email)  
        strstring = mid(strstring, 1, match.firstindex + offset) & link & mid(strstring, match.firstindex + 1 + match.length + offset, len(strstring))  
        offset = offset + len(link) - len(match)  
    next  
    autolinkurls = strstring  
end function

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网