我希望将以下不同的输入类型转换为以下锚标记:
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>
我想我自己可能会写一些类似的东西,但是我想知道是否有一个标准的脚本来做这个已经过彻底的测试?
非常感谢!
答案 0 :(得分:1)
找到这个功能:
Public Shared Function ConvertUrlsToLinks(ByVal msg As String) As String
Dim regex As String = "((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:@=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])"
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=""_blank"">$1</a>").Replace("href=""www", "href=""http://www")
End Function