开发者

Turn URL into Anchor tag using Regular Expression

开发者 https://www.devze.com 2023-01-31 09:21 出处:网络
I want the following different input types to be开发者_Go百科 converted into the following anchor tag:

I want the following different input types to be开发者_Go百科 converted into the following anchor tag:

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

Replaced with:

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

I think I could probably write something similar myself, but I'd like to know if there is a standard script for doing this which has been throughly tested?

Many Thanks!


Found this function:

   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
0

精彩评论

暂无评论...
验证码 换一张
取 消