Replacing all instances of a given string inside an HTML tag

Viewed 64

I am looking for a way to search for a specific string inside an HTML tag and replace it. Specifically, I have an HTML source code in which the task is to search for all instances of class=" inside every <img> tag and replace them with class="lazy .

I am using Notepad++ and wanted to create a macro to handle this. Thus far, I have been able to only isolate the first instance of class=" using the regex expression, but only in the 2nd group:

<img(.*?(\bclass="\b)[^$]*)\/>

Here is the source:

<div class="collapsible-tabs-panel  js-panel" id="v-panel3">
<div class="d-flex"><img alt="azure cloud" class="advance" data-entity-type="file" data-entity-uuid="dab5b799-82be-47d8-8ef4-04408fbcae28" data-src="/sites/default/files/inline-images/azure-cloud-services-5.jpg" src="#" />
<div class="collapsible-tabs-text">
</div>
</div>
</div>

<div class="collapsible-tabs-panel  js-panel" id="v-panel4">
<div class="d-flex"><img alt="inative cloud" data-entity-type="file" data-entity-uuid="c648456a-4f4d-4f68-8766-019603af59e2" data-src="/sites/default/files/inline-images/azure-cloud-services-6.jpg" src="#" />
<div class="collapsible-tabs-text">
</div>
</div>
</div>

<div class="collapsible-tabs-panel  js-panel" id="v-panel5">
<div class="d-flex"><img alt="cloud analytics" class="analyse" data-entity-type="file" data-entity-uuid="cd33a197-e24d-4425-9287-c8d8ddc7afdb" data-src="/sites/default/files/inline-images/azure-cloud-services-7.jpg" src="#" />
<div class="collapsible-tabs-text">
</div>
</div>
</div>

<div><img alt="telecom player" class="astute" data-entity-type="file" data-entity-uuid="99132ab5-943b-4c6c-8a9e-25c2c5dfbe13" data-src="/sites/default/files/inline-images/azure-cloud-services-32.jpg" src="#" /></div>
 
1 Answers

use regex with

img (.*?) class="(.*?)"

and replace with

img \1 class="lazy"

Related