我有几个带有链接的字符串。例如:
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>
最佳答案
一种可能是使用 URI class让它进行解析。类似这样的事情:
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>' )
关于ruby - 提取并添加指向字符串中 URL 的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5993399/