views:

99

answers:

4

Anyone have a clue how I can use Jquery to find: http:// or www. and ends with .com, .org, .edu store that as a variable and wrap it in

<a href="variable">  <a/>

So I have a textarea and when people put in links, I would like to then make them into hyperlinks once they are posted.

Make sense?

+1  A: 

Think you can use regular expressions to do this.

Check this question out. Regular expression to extract URL from an HTML link

It should give you what you need.

griegs
A: 

roll your own way:

var foo = $('textarea selector').val();

then use a regular expression to get what you want. then stick it back into the textarea

$('textarea selector').val('http://thenewlink');

easy plugin way:

7 jQuery Plugins to Manipulate TEXTAREAs

pxl
A: 

lets say the text area contains,

http://site1.com/uri/
http://site2.net/uri
http://site3.org/uri
http://site4.co.uk/uri
http://site5.ws/uri

The first thing i would do is bind an even for keyup on the textarea

$('#addLinks').bind('keyup',function(){
    var _data = $(this).val();
    //Then i would try and split the spaces and carriages 
    _array = _data.split('\n|\r\n|\s|,'); //Split by returns,new lines,spaces,commas.
    var _regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
    $(_array).each(function(value){
       //Now i will check the URL is valid
       if(_regex.test(value))
       {
           //Valid URL so i will then append it to the links container
           //Here you can do e
           $('<a></a>').attr('href',value).val(value).appendTo('.linksContainer');
       }
    })
});

Something along them lines!

Regex borrowed from: http://snippets.dzone.com/posts/show/452

RobertPitt
A: 

Google brought me back to: jQuery Text to Link Script?

utt73