Take the 2-minute tour ×

Hello Stackoverflow community,

I am looking for a regex to match all image URLs (.png|.gif|.bmp|.jpg|.jpeg) between tags.

This is what I had:

var patt1 = /([\s>]|^)((?:http:\/\/)?(?:[a-z][\w-]+)+(?:\.[\w-]+)+(?:\.[\w-]+)*(?:\.[a-z]{2,3})(?:[^ <]+))+[?!.png*$](?=[\s<]|$)/g;
text = text.replace(patt1, "><img class=\"posted-image\" src=\"$2g\" width=\"330px\" height=\"330px\">");

Which works for the most part, but it doesn't match my URL of:

<a href="http://i.lexsym.com/Q.png" target="_newtab">http://i.lexsym.com/Q.png</a>

Could someone perhaps tell me what's going wrong (or just fix it for me ^.^), it would be greatly appreciated.

share|improve this question
It would be safer to make it an actual DOM Object, find the anchors, and read the text. Matching HTML with reg exp is bad. Please read this: stackoverflow.com/questions/1732348/… –  epascarello Oct 17 '12 at 20:44
Except I can't handle the actual DOM Object though, it's just text that happens to contain HTML urls. –  Lexsym Oct 18 '12 at 23:29
add comment

Your Answer



Browse other questions tagged or ask your own question.