$(function() { var re = /(https?:\/\/(([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?))/ig; $('#foo').html($('#foo').html().replace(re, '<a href="$1" title="">$1</a>')); });
If you want the link text with the 'http://' bit, use '<a href="$1" title="">$2</a>' There are scads of URL regexps floating around online. This one works for me but YMMV. On Sun, Jul 26, 2009 at 6:45 PM, Blake<stickynote...@gmail.com> wrote: > > On my site, I would like to have jQuery automatically convert all > plain-text URLs to links. > > For example, if I have the following in the BODY tag of my Web page: > > Go to http://groups.google.com to go to the Google Groups home page. > > I want it to be converted in to this: > > Go to <a href="http://groups.google.com">http://groups.google.com</a> > to go to the Google Groups home page. > > I'm sure the prepend() and append() functions will be used, but how do > I tell jQuery how to pick URLs out of the Web page, if they are not > surrounded by separate tags? I'm sure if each URL was in a SPAN tag it > would be much easier. > > Any ideas? > > Thanks, > stickynote427