I currently have thousands of .html & .htm files which all have a consistent banner at the top of the page. Some of the attributes may be in different locations within the tag, but they all begin with img src=. I want to find the banner - enclosed within an img tag - in all of these files, locate the closing '>'
and then append another image directly after it.
So I want to find
<img src="images/banner.jpg".*>$
and append immediately after it
<img src="images/new-banner.jpg>
so that it would look like
<img src="images/banner.jpg width="x" height="x">
<img src="images/new-banner.jpg">
I know, "regex can't be used to parse HTML" as stated here. But really, I think it should say that regex shouldn't be used to parse HTML, because can't is a powerful word and isn't truthful in this instance.
If you have a recommendation of how I can achieve the same result without regex, I'm happy to try other alternatives. I am after the result and I am not closed off to suggested methods of getting there.
What I current have is this
Select-String -Pattern '<img src="images/banner.jpg".*>$' *.htm -AllMatches | % { $_.Matches } | % { $_.Value }
This gets me halfway there. It returns the found match in its entirety, however, I am not sure how to proceed such that I can append my desired string immediately following the closing >
Thank you all for your time and thoughts :).