0

Search

<img src="images/foo.jpg" alt="" title="caption here" class="figure" />

replace with

<img src="images/foo.jpg" alt="" title="caption here" class="figure" /><figcaption>caption here</figcaption>

or just (my second choice, good enough)

<figcaption>caption here</figcaption>

How do I search and replace with regex? I am using TextMate and Espresso. I am not searching in a HTML file, this HTML is small part of my Drupal post. I learnt I should be using a HTML parser. I believe this case is simpler.

Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132
Jessica K
  • 137
  • 7
  • See the following question about parsing HTML with regex: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags In short, parsing HTML with regex isn't the right tool for the job :) – asf107 Feb 17 '12 at 14:33
  • Yes, I agree. But my use case is unique, I only copy the img tags and insert to textmate/espress to process. Then copy back to my drupal. Thank you for your direction. :) – Jessica K Feb 17 '12 at 15:15

1 Answers1

1

Try searching with:

(<img[^>]+?title=['"])([^'"]*?)(['"][^>]+?>)

and replace with:

$1$2$3<figcaption>$2</figcaption>

sudipto
  • 2,472
  • 1
  • 17
  • 21