【问题标题】:Turn URL into Anchor tag using Regular Expression使用正则表达式将 URL 转换为 Anchor 标签
【发布时间】:2010-12-21 10:27:33
【问题描述】:

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

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>

我想我自己可能会写一些类似的东西,但我想知道是否有一个标准脚本可以做到这一点并且已经过全面测试?

非常感谢!

【问题讨论】:

    标签: .net regex vb.net anchor asp.net-2.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
    

    【讨论】:

      猜你喜欢
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      • 2019-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多