# Callback in the replace method in JavaScript regexes

The replace method as the second parameter can take not only a string, but also a `callback function`, which will be applied for each match found. Each substring that the regex has found will be replaced by what the function returns for this particular substring.

You can pass parameters to this function: the first parameter will contain the found string, the second parameter - the first capturing group, the third parameter - the second capturing group, and so on - you can pass as many parameters as there are capturing groups in the regular expression.

The second-to-last parameter will contain the position of the found match, and the last parameter will contain the entire string to search.

How it all works - let's look at practical examples.

## Example

Let there be a string with numbers:

`let str = '2 3 4 5';`

Let's replace these numbers with their squares. To get started, let's just output our numbers one by one to the console in a callback function:

`str.replace(/\d+/g, function(match) { console.log(match); });`

Our code will output `'2'` first, then `'3'`, `'4'` and `'5'`. That is, the strings found by the regex sequentially fall into the variable `match`.

Let's solve the task to the end - we will square the `match` and return it with `return`. It turns out that for the found `2`, `4` will be returned and the two will be replaced by this four, for the three found, `9` will be returned and the three will be replaced by this nine - and so on:

`let result = str.replace(/\d+/g, function (match) { return match**2; }); console.log(result); // shows '4 9 16 25'`

## Example

Now let a string contain constructions of the form `'2+3='`:

`let str = '2+3= 4+5= 6+7=';`

Let's make it so that the sums of the corresponding numbers are inserted after the equal sign. That is, our string should turn into the following:

`'2+3=5 4+5=9 6+7=13'`

To solve the problem, let's experiment again - put the first and second terms into separate capturing groups:

`str.replace(/(\d+)\+(\d+)=/g, function (match0, match1, match2) { console.log(match0, match1, match2); });`

And now let's finally solve the problem: for each found substring, sum the first and second capturing groups, take the zero capturing group (the found string, for example `'2+3='`), add the result to it and return all this through `return`:

`let result = str.replace(/(\d+)\+(\d+)=/g, function(match0, match1, match2) { let sum = Number(match1) + Number(match2); return match0 + sum; }); console.log(result);`

## Practical tasks

Given a string:

`let str = 'aaa [2] bbb [3] ccc [12] ddd';`

Find the numbers in brackets and double them. That is, from our string we should get the following:

`'aaa [4] bbb [6] ccc [24] ddd'`

Given a string:

`let str = '123 456 789';`

Find all the numbers and put their digits in reverse order. That is, from our string we should get the following:

`'321 654 987'`

Given a string with dates:

`let str = '31.12.2025 30.11.2024 29.10.2023';`

Find all dates and convert them to another format so that you get the following string:

`'2025-12-31 2024-11-30 2023-10-29'`
← →
↑
en ru