这个正则怎么不对?

自动给url加链接。但是空格后的内容也加上了。比如:
https://segmentfault.com/ask 8pp45小幸运
这一行全部加上链接了。
请问怎样修改bug。谢谢。

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  
   
    AutoLinkURLs = strString  
End Function
阅读 1.9k
1 个回答
regExp.Pattern = "(((ht|f)tps?://)|(www\.))([\w-]+\.)+[\w-:]+(/[\w- ./?%#;&=]*)?"

这里最后一段表达式中含有空格[\w- ./?%#;&=]
另外,-不要放在中间
可以改成
regExp.Pattern = "(((ht|f)tps?://)|(www\.))([\w-]+\.)+[\w:-]+(/[\w./?%#;&=-]*)?"