-1

I'm trying to find the regex to replace a block of changing HTML code between two HTML tags:

For example, I want to replace this entire table with a link to an iframe:

  <h2>Older Blog Links</h2>
    <h3>2020</h3>
      <table style="width:100%">
        <tr>
          <td><a href="/media/blog/2020/01/25/20200125.html">Saturday Jan 25</a></td>
          <td><a href="/media/blog/2020/01/19/20200119.html">Sunday Jan 19</a></td>
          <td><a href="/media/blog/2020/01/17/20200117.html">Friday Jan 17</a></td>
          <td><a href="/media/blog/2020/01/15/20200115.html">Wednesday Jan 15</a></td>
        </tr>
      </table>

replace with

 <h2>Older Blog Entries</h2>
        <iframe src="/oldbloglinks.html" width="100%" frameborder="0" onload="resizeIframe(this)"
          style="border: none;"></iframe>

The links in the "find" section above change, so I want to be able to globally replace anything between the <h2> opening tag and the </table> tag

Mark Smith
  • 177
  • 1
  • 7

1 Answers1

1

The regex you're looking for in this case is:

<h2>((.|\n)*)<\/table>

VS Code supports regex base search & replace. Next to the search field, select the .* icon. Run the search and hit the 'replace' button, and you're done!

enter image description here

https://regex101.com/ is a good place to try out your regex against some sample input.

Hoff
  • 34,679
  • 17
  • 65
  • 89
  • Thank you! I was missing the pipe character in everything I had searched and tried. I had

    (\n.*) - What does the pipe do in this instance?

    – Mark Smith Jun 13 '20 at 09:27
  • `(.|\n)*` should NEVER be used. It is the most misfortunate, the least efficient and in some cases just a wrong pattern in the whole regex world. – Wiktor Stribiżew Jun 13 '20 at 10:26
  • 1
    @MarkSmith the pipe mean OR. So 'any character' OR 'a new line' any number of times. – Hoff Jun 13 '20 at 10:30
  • @WiktorStribiżew would you care to explain why it should never be used? It seems to have help the person who asked... – Hoff Jun 13 '20 at 10:31
  • 1
    See https://stackoverflow.com/a/31294276/3832970 and also https://stackoverflow.com/questions/44778923, https://stackoverflow.com/a/33312193/3832970, https://stackoverflow.com/a/46056151/3832970... The list is not comprehensive. You think this "works", you post such an answer, OP is happy, then, in a day or two, the site/code freezes, OP does not understand what is happening. This is the usual scenario when you post such a regex. – Wiktor Stribiżew Jun 13 '20 at 10:42
  • Well, it definitely worked for what I was doing... – Mark Smith Jun 13 '20 at 10:43
  • @MarkSmith Yes, but if your source file is large the backtracking could take a very long time to complete and then it does not work for what you want because you have to wait and wait..... – rioV8 Jun 13 '20 at 16:43
  • 1
    It cycled through 58 source files and did the replacement in less than a second. I have randomly checked all source files and it seems to have worked perfectly – Mark Smith Jun 13 '20 at 16:56
  • @MarkSmith I'm glad it worked for you - that's all I care about:) I don't understand the closing of questions on stackoverflow sometimes... – Hoff Jun 15 '20 at 07:28