提取并添加指向字符串中 URL 的链接

发布于 2024-11-07 05:44:04 字数 281 浏览 0 评论 0原文

我有几个带有链接的字符串。例如:

var str = "I really love this site: http://www.stackoverflow.com"

我需要添加一个链接标签,这样 str 将是:

I really love this site: <a href="http://www.stackoverflow.com">http://www.stackoverflow.com</a>

I have several strings that have links in them. For instance:

var str = "I really love this site: http://www.stackoverflow.com"

and I need to add a link tag to that so the str will be:

I really love this site: <a href="http://www.stackoverflow.com">http://www.stackoverflow.com</a>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

世俗缘 2024-11-14 05:44:04

一种可能是使用 URI 类让它进行解析。类似这样的事情:

require 'uri'

str = "I really love this site: http://www.stackoverflow.com"
url = str.slice(URI.regexp(['http']))
puts str.gsub( url, '<a href="' + url + '">' + url + '</a>' )

One possibility would be to use the URI class to let it do the parsing. Something along the lines of this:

require 'uri'

str = "I really love this site: http://www.stackoverflow.com"
url = str.slice(URI.regexp(['http']))
puts str.gsub( url, '<a href="' + url + '">' + url + '</a>' )
浮生未歇 2024-11-14 05:44:04

您可以使用 URI.extract 来实现这一点:

str = "I really love this site: http://www.stackoverflow.com and also this one: http://www.google.com"

URI.extract(str, ['http', 'https']).each do |uri|
  str = str.gsub( uri, "<a href=\"#{uri}\">#{uri}</a>" )
end

str

上面的代码也匹配一个字符串中的多个 URL。

You can use URI.extract for this:

str = "I really love this site: http://www.stackoverflow.com and also this one: http://www.google.com"

URI.extract(str, ['http', 'https']).each do |uri|
  str = str.gsub( uri, "<a href=\"#{uri}\">#{uri}</a>" )
end

str

The above also matches multiple URLs in one string.

痴情换悲伤 2024-11-14 05:44:04

在这里,工作代码:) 去掉显示链接中的 http/s 前缀,还

请注意,您应该在 uri+" " 上进行正则表达式,以便它正确捕获链接...然后您需要在开头添加一个空格来捕获末尾没有尾随空格的链接...

thisString = yourString+" " # add space to catch link at end
URI.extract(thisString, ['http', 'https']).each do |uri|
  linkURL = uri
  if(uri[0..6] == "http://")
    linkURL = uri[7..-1]
  elsif(uri[0..7] == "https://")
    linkURL = uri[8..-1]
  end
  thisString = thisString.gsub( uri+" ", "<a href=\"#{uri.to_s}\">#{linkURL.to_s}</a> " )
end

Here you go, working code :) strips out the http/s prefix in the displayed link also

note you should regex on uri+" " so it catches the links properly... and then you need to add a space at the beginning to catch links at the end that don't have a trailing space...

thisString = yourString+" " # add space to catch link at end
URI.extract(thisString, ['http', 'https']).each do |uri|
  linkURL = uri
  if(uri[0..6] == "http://")
    linkURL = uri[7..-1]
  elsif(uri[0..7] == "https://")
    linkURL = uri[8..-1]
  end
  thisString = thisString.gsub( uri+" ", "<a href=\"#{uri.to_s}\">#{linkURL.to_s}</a> " )
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文