From: Mark Hubbart Date: 2004-02-15T19:59:22+09:00 Subject: Re: hyperlinking URLs (and emails?) in text This is a little bit more readable, while still being somewhat compact. It also gets plain email addresses: class String def hyperlink # define url parts: chars = Regexp.escape "/!#$\\%&'()*+,.:;=?@~-_" url = "https?:\\/\\/[#{chars}[:alnum:]]+" email = "(?:mailto:)?[^\s@]+@[[:alnum:]._-]+\\.[[:alnum:]._-]+" # do the substitution: gsub(/#{url}|#{email}/) do |link| if link =~ /^[a-zA-Z]+:/ %Q(#{link}) else %Q(#{link}) end end end end Note that this will not get *all* email addresses, and it will let a some urls spill through. But it should get most stuff. Getting it exact would require more lines of code than I want to look at tonight :) On Feb 15, 2004, at 1:27 AM, Ruby Baby wrote: > > Ugly: > > class String > def hyperlink > gsub(/(http:\/\/|https:\/\/|mailto:)([[:alnum:]\/!\#$\%&'()*+,.:;=? > @~-]+)([[:alnum:]\/!\#$\%&'()*+:;=?@~-])/, ' href="\\1\\2\\3">\\1\\2\\3') > end > end > > ... but it generally works, from my few tests. > > > Anyone got a better version, or care to improve on this one? > > Just trying to hyperlink URLs in text (and in future, email addresses, > without needing mailto:). >