我不知道如何(轻松)避免链接(2)来替换链接(1)的开头。我很感激 Ruby 中的答案,但如果你弄清楚逻辑它也很好。
输出应该是:
message = "For Last Minute rentals, please go to:
<span class='external_link' href-web='http://www.mydomain.com/thepage'>http://www.mydomain.com/thepage</span> (1)
For more information about our events, please visit our website:
<span class='external_link' href-web='http://www.mydomain.com'>http://www.mydomain.com</span> (2)"
但它是:
message = "For Last Minute rentals, please go to:
<span class='external_link' href-web='<span class='external_link' href-web='http://www.mydomain.com'>http://www.mydomain.com</span>/thepage'><span class='external_link' href-web='http://www.mydomain.com'>http://www.mydomain.com</span>/thepage</span> (1)
For more information about our events, please visit our website:
<span class='external_link' href-web='http://www.mydomain.com'>http://www.mydomain.com</span> (2)"
这是代码(已编辑:取出跨度):
message = "For Last Minute rentals, please go to:
http://www.mydomain.com/thepage
For more information about our events, please visit our website:
http://www.mydomain.com"
links_found = URI.extract(message, ['http', 'https'])
for link_found in links_found
message.gsub!(link_found,"<span class='external_link' href-web='#{web_link}'>#{link_found}</span>")
end
想法?