Replace only the Nth match
10 May 2025
Here is another practical task. We need to replace only the second or following match in each file.
For example, we want to insert <a name="...">
tags before the second and any subsequent headers in each HTML file. There are multiple headers in each file:

And we want to insert a name to be able to refer to each header. The final result should look like this (the added tag is bold):
<a name="international_search_test"><h2>International search test</h2>
We can solve this task with a lookbehind:

(?<=<h2>.*?)<h2>
This regular expression matches <h2>
, but only if there is another <h2>
before it.
With Aba Search and Replace, there is a more flexible way to do this. You can match all <h2>
tags, but change only the second, the third, etc. tags leaving the first tag intact. The pattern is simple:
<h2>(.*?)</h2>
But in the replacement, we check if the match number is equal to one:
\( if Aba.matchNoInFile() == 1 { \0 } else { '<a name="' \1.replace(' ', '_').toLower() '">' \0 } )
If yes, we return the whole match \0
without any change. If not, we add <a name="...">
before it. We also use the toLower function to convert the name to lowercase and the replace function to replace spaces with underscores.

You can easily modify this one-liner to replace the first three tags in each file only, or replace every second tag, which is more complicated with a lookbehind.
Replacing text in several files used to be a tedious and error-prone task. Aba Search and Replace solves the problem, allowing you to correct errors on your web pages, replace banners and copyright notices, change method names, and perform other text-processing tasks.
This is a blog about Aba Search and Replace, a tool for replacing text in multiple files.
- Replace only the Nth match
- Aba 2.8 released
- Anonymizing a dataset by replacing names with counters
- Automatically add width and height to img tags
- Using zero-width assertions in regular expressions
- Aba 2.7 released
- Regular Expressions 101
- 2023 in review
- Regular expression for numbers
- Aba 2.6 released
- Search from the Windows command prompt
- Empty character class in JavaScript regexes
- Privacy Policy Update - December 2022
- Aba 2.5 released
- Our response to the war in Ukraine
- Check VAT ID with regular expressions and VIES
- Which special characters must be escaped in regular expressions?
- Aba 2.4 released
- Privacy Policy Update - April 2021
- Review of Aba Search and Replace with video
- Aba 2.2 released
- Discount on Aba Search and Replace
- Using search and replace to rename a method
- Cleaning the output of a converter
- Aba 2.1 released
- How to replace HTML tags using regular expressions
- Video trailer for Aba
- Aba 2.0 released