Search
Other ReReplacer questions
Forum

Search and replace in <h1> tag

Stefan Billhardt's Avatar Stefan Billhardt
Hello.
I would like to perform a search and replace in the <h1> tag. It is about all characters up to the first colon. I perform the search with the expression (.*?):. The replacement is done with the expression <small>\1</small><br />. I have activated regular expressions. All other settings are deactivated. The search is limited to the "Body" and should be restricted to <h1> tags. I have activated "Enable in HTML tags" and "Limit to tag selection". For "Tag selection" I have entered h1[*].
The replacement works. But unfortunately everywhere in the body. In all tags in all parameters. I use ReReplacer v14.0.14 [PRO] and Joomla 5.0.2.
Unfortunately I can't get any further at the moment. What am I doing wrong? Any idea?
Regards Stefan
Peter van Westen's Avatar Peter van Westen ADMIN
The "Enable in HTML tags" setting concerns the HTML tags themselves.

So limiting that to h1, will only make the replacement work inside the actual <h1> tag. Not between the opening and closing tags.
So:
<h1 class="my-header">My title</h1>
 ^------------------^
       in there

To do what you want, try this with Regular Expressions:
<h1>([^<]+)\:
Replace with:
<h1><small>\1</small><br />
Please post a rating at the Joomla! Extensions Directory
marvays's Avatar marvays
Hello.
How about if I need the opposite function I have a page code {{ category_name }} and I need to replace it with the first H1 on the page. I try several vaiant but semi fails.
Peter van Westen's Avatar Peter van Westen ADMIN
Not sure how that is the opposite...

If you want to replace a specific tag with (assuming) the contents of the first h1 tag, then the replacement can get pretty heavy. As there might be a lot of html between the 2 pieces of content.

Assuming the {{ category_name }} will be after the h1 in the page:
(<h1>(.*?)</h1>.*)\{\{ category_name \}\}
Replace with:
\1\2
Please post a rating at the Joomla! Extensions Directory
marvays's Avatar marvays
{{ category_name }} will be at the end of HEAD because it is part of the matomo tracking code.
Peter van Westen's Avatar Peter van Westen ADMIN
So before the H1 tag.
\{\{ category_name \}\}(.*?<h1>(.*?)</h1>)
\2\1

Set the Search Area to Everywhere
Please post a rating at the Joomla! Extensions Directory
marvays's Avatar marvays
this doesn't work either. I have already tried countless variants with the help of AI. But without success. The closest I got was that the replacement was correct, but the header was removed from the code 🙁

Use 'newline' modifier?
Peter van Westen's Avatar Peter van Westen ADMIN
Can you give me (super) admin access so I can take a look?
And tell me on what url I can see/reproduce the issue.
You can use the 'Confidential information' button in the forum editor to hide sensitive information.
Please post a rating at the Joomla! Extensions Directory
marvays's Avatar marvays
Confidential information:
(hidden)
Peter van Westen's Avatar Peter van Westen ADMIN
Try this instead:
\{\{ category_name \}\}(.*?<h1[^>]*>(.*?)</h1>)

Issue was it was looking for a simple <h1> and not for a h1 with attributes, like:
<h1 class="navigace-title">
Please post a rating at the Joomla! Extensions Directory
marvays's Avatar marvays
thx. this is working 🙂
You can only post on the extension support forum if you have an active subscription and you log in

Buy a Pro subscription