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?
See the jsFiddle for how to do it, the code you need is in the
replaceWithImgLinksfunctionhttp://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?