pmerino
pmerino

Reputation: 6110

jQuery detect and validate an URL

Let's imagine I have this text:

Hi, my name is Pablo and my home page is http://zad0xsis.net

How can I detect and validate the URL so it automatically wraps it into <a href="the detected url">the detected url</a>? Thanks!

Upvotes: 3

Views: 1304

Answers (2)

Jayendra
Jayendra

Reputation: 52769

Can also try the jquery linkify plugin @ http://code.google.com/p/jquery-linkify/

Easy to use -

$('.section').linkify();

Pretty much does the same as mentioned by Darin.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You could use a regex:

var s = 'Hi, my name is Pablo and my home page is http://zad0xsis.net';
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
s = s.replace(exp,"<a href=\"$1\">$1</a>"); 
alert(s);

Live demo.

Upvotes: 5

Related Questions