Home > Net >  Regex to Match Two Strings Including Anything in Between Without Line-Breaks
Regex to Match Two Strings Including Anything in Between Without Line-Breaks

Time:02-04

I want to replace all the .png extensions in my HTML to .webp so I am doing the regex expressing to match the png links:

\.\/assets\/images\/.*\.png

This works ok if my HTML file has line breaks like this:

<picture>
<source  media="(max-width: 575px)"
srcset="./assets/images/slider/advertisers-pt.png">

<source 
media="(min-width: 576px) and (max-width: 768px)"
srcset="./assets/images/slider/advertisers-pt.png">

<img  srcset="
./assets/images/slider/advertisers-ls.png"
src="./assets/images/slider/advertisers-ls.png" alt="">
</picture>

and it matches all the strings correctly.

but after it's minified, it's no longer working, and it matches the start string until the last occurrence of the second string with everything in between, so the following:

<picture><source  media="(max-width: 575px)"srcset="./assets/images/slider/advertisers-pt.png"><source media="(min-width: 576px) and (max-width: 768px)" srcset="./assets/images/slider/advertisers-pt.png"><img  srcset="./assets/images/slider/advertisers-ls.png" src="./assets/images/slider/advertisers-ls.png" alt=""></picture>

will have a match for:

./assets/images/slider/advertisers-pt.png"><source media="(min-width: 576px) and (max-width: 768px)" srcset="./assets/images/slider/advertisers-pt.png"><img  srcset="./assets/images/slider/advertisers-ls.png" src="./assets/images/slider/advertisers-ls.png

How can I do this with regex after my file is minified?

CodePudding user response:

Try with /S non-whitespace character matcher instead of . matches to any:

\.\/assets\/images\/\S*\.png

regex101 Demo

  • Related