使用正则表达式将URL转换为锚标记

时间:2010-12-21 10:27:33

标签: .net regex vb.net anchor asp.net-2.0

我希望将以下不同的输入类型转换为以下锚标记:

http://www.stackoverflow.com    
https://www.stackoverflow.com    
www.stackoverflow.com

替换为:

<a href='http(s)://www.stackoverflow.com' title='Link opens in new window' target='_blank'>www.stackoverflow.com</a>

我想我自己可能会写一些类似的东西,但是我想知道是否有一个标准的脚本来做这个已经过彻底的测试?

非常感谢!

1 个答案:

答案 0 :(得分:1)

找到这个功能:

   Public Shared Function ConvertUrlsToLinks(ByVal msg As String) As String
        Dim regex As String = "((www\.|(http|https|ftp|news|file)+\:\/\/)[&#95;.a-z0-9-]+\.[a-z0-9\/&#95;:@=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])"
        Dim r As New Regex(regex, RegexOptions.IgnoreCase)
        Return r.Replace(msg, "<a href=""$1"" title=""Click to open in a new window or tab"" target=""&#95;blank"">$1</a>").Replace("href=""www", "href=""http://www")
    End Function
相关问题