<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Regex to remove specific classes from specific tags in HTML code]]></title><description><![CDATA[<p dir="auto">I need to use regular expressions to remove specific classes from specific tags in HTML code. For example: remove class <strong>fred</strong> from tag <strong>p</strong> only.</p>
<p dir="auto"><code>&lt;p class="matt fred"&gt;some text&lt;/p&gt; → &lt;p class="matt"&gt;some text&lt;/p&gt;</code><br />
<code>&lt;h1 class="matt fred"&gt;some text&lt;/h1&gt; → &lt;h1 class="matt fred"&gt;some text&lt;/h1&gt;</code><br />
<code>&lt;p class="fred"&gt;some text&lt;/p&gt; → &lt;p&gt;some text&lt;/p&gt;</code><br />
<code>&lt;p id="fred"&gt;some text&lt;/p&gt; → &lt;p id="fred"&gt;some text&lt;/p&gt;</code><br />
<code>&lt;p class="matt fred jane"&gt;some text&lt;/p&gt; → &lt;p class="matt jane"&gt;some text&lt;/p&gt;</code><br />
<code>&lt;p class="fred jane"&gt;some text&lt;/p&gt; → &lt;p class="jane"&gt;some text&lt;/p&gt;</code></p>
<p dir="auto">Please, note that a tag may have one or more classes and that the class to be removed can be in any position, so it is necessary to manage space separators too.</p>
<p dir="auto">Thank you in advance.</p>
]]></description><link>https://community.notepad-plus-plus.org/topic/13316/regex-to-remove-specific-classes-from-specific-tags-in-html-code</link><generator>RSS for Node</generator><lastBuildDate>Sun, 16 Aug 2026 16:42:41 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/13316.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 18 Feb 2017 07:42:28 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Regex to remove specific classes from specific tags in HTML code on Mon, 20 Feb 2017 21:12:58 GMT]]></title><description><![CDATA[<p dir="auto">Hi, <strong>Dario</strong></p>
<p dir="auto">At the <strong>end</strong> of my <strong>previous</strong> post, I said :</p>
<blockquote>
<p dir="auto">If you don’t want to <strong>repeat</strong> the word <strong>Fred</strong>, <strong>several</strong> times in the search regex,…</p>
</blockquote>
<p dir="auto">and I gave a second, but rather <strong>complicated</strong>, regex !</p>
<p dir="auto">Thinking about it, here is a <strong>third</strong> and more <strong>simple</strong> regex, for the <strong>same</strong> purpose :</p>
<p dir="auto">SEARCH <strong><code>(?-s)&lt;p[^&lt;&gt;\r\n]*(\K class="(fred)"| class=".+\K (?2)| class="\K(?2) )</code></strong></p>
<p dir="auto">REPLACE <strong><code>Leave EMPTY</code></strong></p>
<p dir="auto">Remember that the <strong><code>(?n)</code></strong> syntax, called a <strong>subroutine call</strong>, represents the regex enclosed in the <strong>nth</strong> pair of <strong>round</strong> brackets, when going through the regex, from <strong>left</strong> to <strong>right</strong> and can be placed <strong>after</strong> of <strong>before</strong> the group to which it <strong>refers</strong> !</p>
<p dir="auto">In our example, the regex is, simply, the string <strong>fred</strong> and the <strong><code>(?2)</code></strong> forms are located <strong>after</strong> the <strong>second</strong> group <strong><code>(fred)</code></strong></p>
<hr />
<p dir="auto">It’s very <strong>important</strong> to note that the <strong>similar</strong> regex, which uses <strong>back-references</strong>, does <strong>not</strong> work ! Indeed, the regex :</p>
<p dir="auto"><strong><code>(?-s)&lt;p[^&lt;&gt;\r\n]*(\K class="(fred)"| class=".+\K \2| class="\K\2 )</code></strong>, matches, <strong>only</strong>, the <strong>two</strong> lines, below :</p>
<pre><code class="language-css">&lt;p class="fred"&gt;some text&lt;/p&gt;
&lt;p id="12345" class="fred"&gt;some text&lt;/p&gt;
</code></pre>
<p dir="auto">Why ? Well, these <strong>positive</strong> matchs concern the <strong>first</strong> case of the <strong>alternative</strong>. When it tries to match the <strong>two other</strong> cases of the <strong>alternative</strong> ( " <code>class=".+\K \2</code>" or " <code>class="\K\2</code> " ) the <strong>group 2</strong> does <strong>not</strong> exist, as the <strong>first</strong> part of the alternative is <strong>not</strong> used !!</p>
<p dir="auto">Cheers,</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/22035</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/22035</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Mon, 20 Feb 2017 21:12:58 GMT</pubDate></item><item><title><![CDATA[Reply to Regex to remove specific classes from specific tags in HTML code on Mon, 20 Feb 2017 20:16:25 GMT]]></title><description><![CDATA[<p dir="auto">Hello <strong>Dario</strong></p>
<p dir="auto">No problem ! So, given your <strong>original</strong> example, to whom I <strong>added</strong> some <strong>possible</strong> cases :</p>
<pre><code class="language-css">&lt;p class="matt fred"&gt;some text&lt;/p&gt;
&lt;p id="12345" class="matt fred"&gt;some text&lt;/p&gt;

&lt;h1 class="matt fred"&gt;some text&lt;/h1&gt;
&lt;h1 id="12345" class="matt fred"&gt;some text&lt;/h1&gt;

&lt;p class="fred"&gt;some text&lt;/p&gt;
&lt;p id="12345" class="fred"&gt;some text&lt;/p&gt;

&lt;p id="fred"&gt;some text&lt;/p&gt;
&lt;p type="xxx" id="fred"&gt;some text&lt;/p&gt;

&lt;p class="matt fred jane"&gt;some text&lt;/p&gt;
&lt;p id="12345" class="matt fred jane"&gt;some text&lt;/p&gt;

&lt;p class="fred jane"&gt;some text&lt;/p&gt;
&lt;p id="12345" class="fred jane"&gt;some text&lt;/p&gt;
</code></pre>
<p dir="auto">I propose the following <strong>S/R</strong> :</p>
<p dir="auto">SEARCH <strong><code>(?-s)&lt;p[^&lt;&gt;\r\n]*(\K class="fred"| class=".+\K fred| class="\Kfred )</code></strong></p>
<p dir="auto">REPLACE <strong><code>Leave EMPTY</code></strong></p>
<p dir="auto"><strong>REMARKS</strong> :</p>
<ul>
<li>
<p dir="auto">In the <strong>search</strong> regex, there is a <strong>space</strong> character :</p>
<ul>
<li>
<p dir="auto">Before <strong>each</strong> word <strong>class</strong></p>
</li>
<li>
<p dir="auto">Before the <strong>second</strong> occurrence of the word <strong>fred</strong></p>
</li>
<li>
<p dir="auto">After the <strong>third</strong> occurrence of the word <strong>fred</strong></p>
</li>
</ul>
</li>
<li>
<p dir="auto">The <strong>Regular expression</strong> search mode needs to be <strong>set</strong>, of course !</p>
</li>
<li>
<p dir="auto">You must use the <strong>Replace All</strong> button, <strong>exclusively</strong> ( The simple <strong>step by step</strong> replacement, with the <strong>Replace</strong> button, does nothing, due to an <strong>incorrect</strong> handling of <strong>look-behinds</strong> and <strong>backward</strong> assertions ! )</p>
</li>
</ul>
<hr />
<p dir="auto">After clicking on the <strong>Replace All</strong> button,  you should get the <strong>changed</strong> text, below :</p>
<pre><code class="language-css">&lt;p class="matt"&gt;some text&lt;/p&gt;
&lt;p id="12345" class="matt"&gt;some text&lt;/p&gt;

&lt;h1 class="matt fred"&gt;some text&lt;/h1&gt;
&lt;h1 id="12345" class="matt fred"&gt;some text&lt;/h1&gt;

&lt;p&gt;some text&lt;/p&gt;
&lt;p id="12345"&gt;some text&lt;/p&gt;

&lt;p id="fred"&gt;some text&lt;/p&gt;
&lt;p type="xxx" id="fred"&gt;some text&lt;/p&gt;

&lt;p class="matt jane"&gt;some text&lt;/p&gt;
&lt;p id="12345" class="matt jane"&gt;some text&lt;/p&gt;

&lt;p class="jane"&gt;some text&lt;/p&gt;
&lt;p id="12345" class="jane"&gt;some text&lt;/p&gt;
</code></pre>
<p dir="auto"><strong>NOTES</strong> :</p>
<ul>
<li>
<p dir="auto">The <strong><code>(?-s)</code></strong> syntax, beginning the regex, ensures that any <strong>dot</strong> will match a <strong>single standard</strong> character, only and <strong>NOT</strong> an <strong>End of Line</strong> character</p>
</li>
<li>
<p dir="auto">Then the part <strong><code>&lt;p[^&lt;&gt;\r\n]*</code></strong> looks for the string <strong>&lt;p</strong>, followed by a range, even <strong>null</strong>, of characters, <strong>different</strong> from <strong><code>&lt;</code></strong>, <strong><code>&gt;</code></strong>, <strong><code>\r</code></strong> and <strong><code>\n</code></strong>, till, either, <strong>each</strong> part of the <strong>alternative</strong> sequence <strong><code>(....|....|....)</code></strong> :</p>
<ul>
<li>
<p dir="auto">The string <strong><code> class="fred"</code></strong>, beginning with a <strong>space</strong> character. As this string is <strong>preceded</strong> by the <strong><code>\K</code></strong> syntax, this means that the regex engine position is <strong>reset</strong> and that everything matched <strong>before</strong> that string is <strong>forgotten</strong>. So, this <strong>whole</strong> string will be <strong>deleted</strong>, during the <strong>replacement</strong> phase</p>
</li>
<li>
<p dir="auto">The string <strong><code> class=".+\K fred</code></strong>, beginning with a <strong>space</strong> which contains a <strong>non-null</strong> range of <strong>standard</strong> characters, till the string <strong>fred</strong>, preceded by a <strong>space</strong>. Again, due to the <strong><code>\K</code></strong> form, this <strong>later</strong> string will be <strong>deleted</strong></p>
</li>
<li>
<p dir="auto">The string <strong><code> class="\Kfred )</code></strong>, beginning with a <strong>space</strong> and ending with the string <strong>fred</strong> and a <strong>space</strong> character. Again, because of the <strong><code>\K</code></strong> syntax, only the string  <strong>fred</strong> and the <strong>space</strong> character, will be <strong>suppressed</strong>, during replacement</p>
</li>
</ul>
</li>
</ul>
<hr />
<p dir="auto">If you don’t want to <strong>repeat</strong> the word <strong>Fred</strong>, several times in the search regex, here is a <strong>second</strong> solution :</p>
<p dir="auto">SEARCH <strong><code>(?(DEFINE)(fred))(?-s)&lt;p[^&lt;&gt;\r\n]*(\K class="(?1)"| class=".+\K (?1)| class="\K(?1) )</code></strong></p>
<p dir="auto">REPLACE <strong><code>Leave EMPTY</code></strong></p>
<p dir="auto">This construction is a special <strong>conditional</strong> regex syntax. Refer to the two links, below, for <strong>further</strong> information :</p>
<p dir="auto"><a href="http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html#boost_regex.syntax.perl_syntax.conditional_expressions" rel="nofollow ugc">http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html#boost_regex.syntax.perl_syntax.conditional_expressions</a></p>
<p dir="auto"><a href="http://www.regular-expressions.info/subroutine.html#define" rel="nofollow ugc">http://www.regular-expressions.info/subroutine.html#define</a></p>
<hr />
<p dir="auto">To end, as you said :</p>
<blockquote>
<p dir="auto">Please, note that a tag may have one or more classes and that the class to be removed can be in any position, so it is necessary to manage space separators too.</p>
</blockquote>
<p dir="auto">Just tell me about all the <strong>missing</strong> cases which may occur, in your <strong>HTML</strong> text and I’ll try to <strong>improve</strong> the regex :-)</p>
<p dir="auto">Best Regards,</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/22017</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/22017</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Mon, 20 Feb 2017 20:16:25 GMT</pubDate></item></channel></rss>