1

Current Textarea:

<textarea id="event_content">
This text area could be full of information..www.london2012.com
And might contain upto 5 links that all need updating.
www.rio2016.org

Link already converted. this should be left.
<a href="http://www.thetimes.co.uk" target="_blank">www.thetimes.co.uk</a>
</textarea>

Desired Textarea after jquery: all links to be cleaned/replaced with tags and target attr

<textarea id="event_content">
This text area could be full of information
<a href="http://www.london2012.com" target="_blank">www.london2012.com</a>
And might contain upto 5 links that all need updating.
<a href="http://www.rio2016.org" target="_blank">www.rio2016.org</a>

Link already converted. this should be left.
<a href="http://www.thetimes.co.uk" target="_blank">www.thetimes.co.uk</a>
</textarea>

21 July 2012 - Ωmega's code worked a treat thanks, but could be improved by leaving already converted links?

Ωmega
  • 42,614
  • 34
  • 134
  • 203
Harry
  • 1,509
  • 3
  • 15
  • 25
  • 1
    Your original question was simple, this is somewhat trickier... have a look at using a Regex to identify the url's in the body of the text, then replace them in a similiar manner to Cristy's answer. This question might help http://stackoverflow.com/questions/5461702/regex-to-find-url-in-a-text – magritte Jul 20 '12 at 14:56
  • Yes regex would do it. I can do it in php but not jquery?? preg_replace('!(http://[a-z0-9_./?=&-]+)!i', '$1 ', $text." "); – Harry Jul 20 '12 at 15:42

4 Answers4

9

I believe you are looking for something like this (click here to test this fiddle):

$('#event_content').val(
  $('#event_content').val().replace(/\b(http(s|):\/\/|)(www\.\S+)/ig,
    "<a href='http\$2://\$3' target='_blank'>\$3</a>"));
Ωmega
  • 42,614
  • 34
  • 134
  • 203
  • Yes I believe that is the code I was looking for! Could your regex be tweaked to not include links already converted? – Harry Jul 21 '12 at 08:13
  • 1
    @Harry - That would have to be a loop going through all links with storing previous matches and conditional regex replace. – Ωmega Jul 21 '12 at 10:47
1
var link = $("#event_content");
var text = link.html();
var linktext = '<a href="' + text + '" target="_blank">' + text + '</a>'
link.html(linktext);
Wouter Janssens
  • 1,593
  • 10
  • 17
1
$(function(){
    var old = $('#event_content').val();

    var news = '<a href="http://'+old+'" target="_blank">'+old+'</a>';

    $('#event_content').val(news);
});

Note that the <a> will not be displayed in textarea, but instead plain text will be shown.

Demo: http://jsfiddle.net/3xEh2/1/

XCS
  • 27,244
  • 26
  • 101
  • 151
  • Good thanks but there is other info in the textarea so I have updated the original question. – Harry Jul 20 '12 at 14:42
1

fiddle

This will replace all the links and leave the other text there as well.

$(function(){
var old = $('#event_content').val();
var news = old.replace("www.london2012.com", '<a href="http://www.rio2016.com" target="_blank">www.rio2016.com</a>');   
$('#event_content').val(news);
});
Mark K
  • 581
  • 1
  • 3
  • 13