Replacements in Joomla with ReReplacer and Regular Expressions

rereplacer regex joomla

ReReplacer by RegularLabs is an extremely popular extension. It helps you easily replace almost any content in Joomla. We have a beginner tutorial on using ReReplacer.

This is a more advanced tutorial. In this tutorial, you will learn how to update elements on your site that are similar, but not exactly the same. You can do that with the ReReplacer component and regular expressions.

Step #1. Download and Install

  • Go to the ReReplacer page.
  • Click the Download Free button.
  • Download the extension.
download rereplacer
  • Login to your Joomla administrator area.
  • Go to Extensions > Manage.
  • Click the Upload Package File tab.
  • Find the zip file you just downloaded. Click twice on the file name to install ReReplacer.

Step #2. Example of a Use Case

I have several articles about singers. Each article has a link at the bottom to buy their music album.

All those links use the same structure, the only difference is the singer's name: "Buy singer Album". Here's an example with Amy Winehouse:

rereplacer regex joomla
Here's an example with Christina Perri:
rereplacer regex joomla

I want to add an icon at the beginning of those links and also change the artist's name to use bold text.

Step #3. Simple Replacement

Using ReReplacer, you can quickly make these changes globally, that is sitewide.

  • Go to Components > Regular Labs - ReReplacer:

components regularlabs rereplacer

  • You will see the administrative ReReplacer: List page. Click New.

click new

  • Fill the Search and the Replace boxes as follows (note: every replacement is separated by a comma).
rereplacer regex joomla
  • Set the Treat as list parameter to Yes.
  • Click Save & Close.

I'll preview my site to confirm the replacement works on all the articles.

rereplacer regex joomla
rereplacer regex joomla

If we have a few content items to replace, this method is enough. However what if we have more than 100 buttons to replace? It would be an exhaustive work. In that case, try this next approach.

Step #4. Regular Expression

Edit the previous example:

  • Fill in the Search and the Replace fields as follows:
rereplacer regex joomla
  • Set the Regular Expressions setting to Yes.
rereplacer regex joomla
  • Save your changes.

You will have the exact same result from step 3, but notice that this time you're using a short syntax.

If in future you add a new article with a link that matches the regular expression, it will apply the replacement automatically.

In the search syntax, (.*?) acts as a dynamic value to find every instance of "Buy ... "Album".

Buy (.*?) Album

Now, we can replace the contents of (.*?). We use \1 because it is the first and only regular expression we're using. Here's the new syntax:

<span class="icon-cart"></span> Buy <strong>\1</strong> Album