Rails used to have a built in "auto_link" function, which now can be brought in as an 'autolink' gem. This does other string sanitization too.
Alternatively the 'anchored' gem is more focused on this one task.
Without gems, here's the ruby code I have used:
url_regexp = %r{
(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)
(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|
[-A-Z0-9+&@#\/%=~_|$?!:,.])*
(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|
[A-Z0-9+&@#\/%=~_|$])
}ix
text = text.gsub(url_regexp, '<a href="\0">\0</a>').html_safe
I recommend you carefully test some edge cases with whatever solution you go for. Looking around for answers to this you'll find people recommending the core library URI.regexp
or URI::DEFAULT_PARSER
, but this fails very simple tests. Try including a word followed by a colon ':' char in your text. You don't want that to become a link! (I don't really know why this built-in solution is so poor. My guess that is it's more intended for validating rather than finding URL-like substrings)
So my solution above is a regexp based on this answer and converted to be more rubyish. But when you start looking, there are many different regexps for URLs out there. You can play the game of finding the awkward edge case that any given regexp fails on. Generally longer regexps catch more edge cases. But there's also a challenge to convert it to work in ruby. This website presents many language solutions including a (quite long) ruby example: http://urlregex.com