-1

Possible Duplicate:
How to replace plain URLs with links?

So I've got a page with a bunch of URLs and no links. The page is constantly pulling in new information, so I can't add the tags myself. Where could I find a fast, lightweight, JavaScript snippet or jQuery plugin to linkify these URLs?

Turn http://blahblah.com/

into <a href="http://blahblah.com/">http://blahblah.com/</a>

Community
  • 1
  • 1
alt
  • 13,357
  • 19
  • 80
  • 120
  • 1
    I can foresee some regex coming. – Fabrício Matté Jul 20 '12 at 02:39
  • Those functions in the solutions require the input of specific text, I need to be able to specify an element and have all of its contained text be changed. – alt Jul 20 '12 at 02:44
  • 1
    Just pass the data received from the ajax to it before appending the linkified version? Or grab the element's html with `.html()` and replace it with the linkified version.. – Fabrício Matté Jul 20 '12 at 02:45
  • THis yields no dice: `replaceURLWithHTMLLinks($('li').text())` (regarding the first solution here: http://stackoverflow.com/questions/37684/how-to-replace-plain-urls-with-links) – alt Jul 20 '12 at 02:47
  • `$('li').html(replaceURLWithHTMLLinks($('li').html()))` or, if you have more than one `li`, `$('li').each(function() { $(this).html(replaceURLWithHTMLLinks($(this).html())); });` – Fabrício Matté Jul 20 '12 at 02:51
  • Regexps are brittle and should be avoided. I've summarized [the best autolinkification libraries in another answer](http://stackoverflow.com/a/21925491/1269037). – Dan Dascalescu Feb 21 '14 at 12:58

1 Answers1

1

With JQuery:
Assuming your links are in p (for performance, use the closest selector)

$(document).ready(function(){
    $('p').each(function(){
        var content = $(this).html();
        content = content.replace(/[^"=](http:\/\/\S*)/ig, '<a href="$1">$1</a>');
        $(this).html(content);
    });
});​

Surely not the most optimized way, and the regex is not really well-formed but at least it doesn't replaces URLs that are in tags attributes (like images src).

Bali Balo
  • 3,338
  • 1
  • 18
  • 35
  • Regexps are brittle and should be avoided. I've summarized [the best autolinkification libraries in another answer](http://stackoverflow.com/a/21925491/1269037). – Dan Dascalescu Feb 21 '14 at 12:58