1

I use this function for transforming URLs to images on output.

function InsertLink(T){
 var Out = '';
 var T1=T;
 var LinkR = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?([^?#]*\.(?:jpg|jpeg|gif|png))/;
 var Pos=0;
 for(var N=0;(N=T1.search(LinkR))!==-1;){
    var S1 = T1.match(LinkR)[0];
    var S1L = S1.length;
    Out += T1.substr(0,N)+"<a href='"+S1+"' target='_blank'><img class='sml' src='"+S1+"' /></a><br />";
    T1 = T1.substr(N+S1L);
    Pos=N+S1L;
 };
 Out+=T1;
 return Out;
}

But it working only for one URL in post body. If text contains more than one URL, all URLs attached to one image ("broken" image).

What's wrong?

Sir D
  • 2,164
  • 2
  • 17
  • 21

1 Answers1

2

See the jsFiddle for how to do it, the code you need is in the replaceWithImgLinks function

http://jsfiddle.net/C3zF6/1/

I had trouble getting your regex to work so I used the one from here: What is a good regular expression to match a URL?

Community
  • 1
  • 1
sparebytes
  • 12,546
  • 3
  • 21
  • 32