{"id":213,"date":"2025-01-28T04:46:24","date_gmt":"2025-01-28T04:46:24","guid":{"rendered":"https:\/\/www.kwwd.co.uk\/blog\/?p=213"},"modified":"2025-01-28T04:47:48","modified_gmt":"2025-01-28T04:47:48","slug":"regex-to-replace-empty-tags-with-just-ids-using-notepad","status":"publish","type":"post","link":"https:\/\/www.kwwd.co.uk\/blog\/regex-to-replace-empty-tags-with-just-ids-using-notepad\/","title":{"rendered":"RegEx To Replace Empty Tags with just IDs Using Notepad++"},"content":{"rendered":"<p>Here is an example html output from a DOCX file imported into WordPress (in this case with the Mammoth docx converter plugin):<\/p>\n<p>&lt;h2&gt;&lt;a id=&#8221;post-44034-_7ttglipb267h&#8221;&gt;&lt;\/a&gt;Header Text&lt;\/h2&gt;<br \/>\nSome Content here<br \/>\n&lt;h3&gt;&lt;a id=&#8221;post-44034-_lx7dlbt1tney&#8221;&gt;&lt;\/a&gt;Sub Header Text&lt;\/h3&gt;<br \/>\nSome Content here<\/p>\n<p>The IDs will obviously vary but the common factor is that the &lt;a&gt; contains no content.<\/p>\n<p>If we&#8217;re not referencing the headings in our own table of contents, we don&#8217;t need this code &#8211; and if we&#8217;re using a Plugin to create a TOC it will create its own IDs as required so we can remove these using Notepad++ using a RegEx (regular expression) in the Replace Dialog:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-215 size-full\" src=\"https:\/\/www.kwwd.co.uk\/blog\/wp-content\/uploads\/2025\/01\/window-04-43-48-notepad-new_1_-_Notepad.png\" alt=\"The Notepad++ Find and replace showing our RegEX\" width=\"627\" height=\"386\" srcset=\"https:\/\/www.kwwd.co.uk\/blog\/wp-content\/uploads\/2025\/01\/window-04-43-48-notepad-new_1_-_Notepad.png 627w, https:\/\/www.kwwd.co.uk\/blog\/wp-content\/uploads\/2025\/01\/window-04-43-48-notepad-new_1_-_Notepad-300x185.png 300w\" sizes=\"auto, (max-width: 627px) 100vw, 627px\" \/><\/p>\n<p>Ensure that you have checked the &#8220;RegEx&#8221; option on the bottom left search mode of the search\/replace dialog and then search for the following:<\/p>\n<pre><code class=\"language-php\">&lt;a[^&gt;]*&gt;&lt;\/a&gt;<\/code><\/pre>\n<p><strong>Explanation<\/strong>:<\/p>\n<ul>\n<li><code>&lt;a<\/code>: Matches the opening <code>&lt;a&gt;<\/code> tag.<\/li>\n<li><code>[^&gt;]*<\/code>: Matches any attributes inside the <code>&lt;a&gt;<\/code> tag (if any).<\/li>\n<li><code>&gt;&lt;\/a&gt;<\/code>: Matches the closing <code>&lt;\/a&gt;<\/code> tag, ensuring the tag has no content between them.<\/li>\n<\/ul>\n<p>This regex will find all <code>&lt;a&gt;<\/code> tags with no content inside them and can be used to remove those unwanted tags.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is an example html output from a DOCX file imported into WordPress (in this case with the Mammoth docx converter plugin): &lt;h2&gt;&lt;a id=&#8221;post-44034-_7ttglipb267h&#8221;&gt;&lt;\/a&gt;Header Text&lt;\/h2&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39],"tags":[44,45,43],"class_list":["post-213","post","type-post","status-publish","format-standard","hentry","category-tips-tricks","tag-notepad","tag-regex","tag-replace"],"_links":{"self":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts\/213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=213"}],"version-history":[{"count":0,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts\/213\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}