-3

if anyone knows is plug-in who can find links on div and make clickable? I have div with text:

<div id="mytext">Wow, please go to http://www.google.com - this is my home page ;)</div>

And I want make automatic clickable links.

Kuba Żukowski
  • 653
  • 3
  • 11
  • 16
  • this is actually interesting, I'm pretty sure you won't find a plugin out there, but you can make a function that does that – Andrei Cristian Prodan Mar 07 '14 at 16:18
  • Use `replace` and the search bar to look for a regex. –  Mar 07 '14 at 16:19
  • I don't know one, SO [does however](http://stackoverflow.com/questions/37684/how-to-replace-plain-urls-with-links). Gotta love searching... – giorgio Mar 07 '14 at 16:22
  • 2
    See [jquery's linkify](http://soapbox.github.io/jQuery-linkify/). Better than reinventing the wheel because there are a million edge cases unless you only intend to work on a specific subset of data. –  Mar 07 '14 at 16:23

1 Answers1

0

An easy to follow implementation:

Fiddle

var url_pattern = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/;

var content = $("#mytext").text();
var newContent = content.replace(url_pattern, function (link) {
    return '<a href="' + link + '" target="_blank">' + link + '</a>';
});
$('#mytext').html(newContent);
Tomanow
  • 7,247
  • 3
  • 24
  • 52