Constructing a regular expression to wrap images with <a>
- by bobo
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?