URL to image transformation
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? 

URL to image transformation
===

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? 
