How to use regex to replace text between tags in Notepad++

notepad++, regex, replace

Solution

This regex will match the parts of the anchor tag you need to put back:

<pre><code>([^<]*?)<a href="(.*?)">(.*?)</a>(.*?)</code></pre>

See a live demo, which shows it matching correctly and also shows the various parts being captured as groups which we'll refer to in the replacement string (see below).

Use the regex above with the following replacement:

<pre><code>\1&lt;a href=&quot;\2&quot;&gt;\3&lt;/a&gt;\4</pre></code>

The `\1`, `\2` etc are the captured groups in the regex that put back what we're keeping from the match.

Problem

I have a code like this: ``` <pre><code>Some <a href="">HTML</a> code</code></pre> ``` I need to escape the HTML between the `<pre><code></code></pre>` tags. I have lots of tags, so I thought - why not let regex do it for me. The problem is I don't know how. I've seen lots of examples using Google and Stackoverflow, but nothing I could use. Can someone here help me? Example: ``` <pre><code>Some <a href="http">HTML</a> code</code></pre> ``` To ``` <pre><code>Some &lt;a href=&quot;http&quot;&gt;HTML&lt;/a&gt; code</code></pre> ``` Or just a regex so I can replace anything between the `<pre><code>` and `</code></pre>` tags one by one. I'm almost certain that this can be done.

Original source