Find images that have a certain HTML class name

Posted by Frederik Vig on Stack Overflow See other posts from Stack Overflow or by Frederik Vig
Published on 2010-03-12T09:12:03Z Indexed on 2010/03/12 9:17 UTC
Read the original article Hit count: 178

Filed under:
|
|

I have some markup that contains certain HTML image tags with the class featured. What I need is to find all those images, add an anchor tag around the image, set the href attribute of the anchor to the images src value (the image path), and lastly replace the images src value with a new value (I call a method that will return this value).

<p>Some text here <img src="/my/path/image.png" alt="image description" class="featured" />. Some more text and another image that should not be modified <img src="/my/path/image2.png" alt="image description" /></p>

Should become.

<p>Some text here <a href="/my/path/image.png"><img src="/new/path/from/method.png" alt="image description" class="featured" /></a>. Some more text and another image that should not be modified <img src="/my/path/image2.png" alt="image description" /></p>

© Stack Overflow or respective owner

Related posts about regex

Related posts about c#