Search code examples
htmlreplacetags

How to change HTML tags preserving its contained content


Across multiple html files I have 217 instances of:

<p class="quote">“Lots of different quotes.”</p>

And want to bulk change them into:

<blockquote>
  <p>“Lots of different quotes.”</p>
</blockquote>

I was thinking of using regular expressions, until I came across this answer, which discourages the use of regex. But the answer is quite old so I wonder if there is a new best approach.

I tried regex but I am not sure whether that is the most efficient method. I am using vscodium, but didn't immediately find relevant extensions.


Solution

  • Using VSCodium the following regular expression captured most cases by replacing:

    <p class="quote">([^<]+)</p>
    

    or the following to include nested tags

    <p class="quote">((?:[^<]+|<(?!/p>))*)</p>
    

    with

    <blockquote>\n\t\t\t<p>$1</p>\n\t\t</blockquote>