Constructing a regular expression to wrap images with <a>
Posted
by bobo
on Stack Overflow
See other posts from Stack Overflow
or by bobo
Published on 2010-04-22T07:09:23Z
Indexed on
2010/04/22
7:13 UTC
Read the original article
Hit count: 205
A web page contains lots of image elements:
<img src="myImage.gif" width="180" height="18" />
But they may not be very well-formed, for example, the width or height attribute may be missing. And it also may not be properly closed with /. The src attribute is always there.
I need a regular expression that wraps these with a hyperlink having href set to the src of the img.
<a href="myImage.gif" target="_blank"><img src="myImage.gif" width="180" height="18" /></a>
I can successfully locate the images using this regexp in this editor: http://gskinner.com/RegExr/:
<img src="([^<]*)"[^<]*>
But what is the next step?
© Stack Overflow or respective owner