Level 2: Practical patterns · Lesson 20 of 30
Replace with $1
Capture groups let a replacement reuse pieces of the match. Write $1, $2 and friends in the replacement string to rearrange text.
Search and replace becomes far more powerful once you combine it with capture groups. Every pair of plain parentheses in a pattern records what it matched, and the replacement string can refer to those recordings by number. In JavaScript, $1 means 'the text captured by group 1', $2 means group 2, and so on.
Suppose you have dates written as 2026-11-30 and you want 30/11/2026. The pattern (\d{4})-(\d{2})-(\d{2}) captures the year, month and day. The replacement $3/$2/$1 puts them back in the new order. Every date in the text is rewritten in one pass, and anything that does not match is left alone.
Groups are numbered by the position of their opening parenthesis, counting from left to right. Nested groups count too: in ((\w+)@), the outer group is 1 and the inner one is 2. Non-capturing groups (?:...) are skipped, which is a strong reason to use them for structure.
There are a few other special tokens in JavaScript replacements. $& inserts the entire match, which is useful for wrapping text, as in replacing \d+ with [$&]. $<name> inserts a named group, and $$ inserts a literal dollar sign. If you reference a group that does not exist, JavaScript inserts the text literally, so double-check your numbering.
Other languages use slightly different replacement syntax. Python writes \1 or \g<1> in the replacement. PHP accepts $1 and \1. Java and C# use $1. RegexLab's Replace mode uses JavaScript rules and shows a live preview, so you can see the result before copying the code for your language.
Rearranging names is another classic job. The pattern (\w+), (\w+) with replacement $2 $1 turns Test, Ali into Ali Test. Inserting separators works the same way: ([A-Z]+)(\d+) with $1-$2 turns AB12 into AB-12.
When designing a replacement, first make sure the pattern matches exactly the spans you want to rewrite, then decide which pieces need capturing. The practice tasks below check the match itself; try each solution in Replace mode with a replacement of your choice.
Live tester
2 matches (JavaScript engine)
Contacts: ali@shop.test and sara@mail.test
Examples
Numbers
/(\d{4})-(\d{2})-(\d{2})/Due 2026-11-30 and 2026-12-15.
Matches: “2026-11-30”, “2026-12-15”
Replace with `$3/$2/$1` to turn ISO dates into day/month/year order.
Text
/(\w+), (\w+)/Test, Ali; Demo, Sara
Matches: “Test, Ali”, “Demo, Sara”
Replace with `$2 $1` to flip last-name-first entries into first-name-first.
Codes
/([A-Z]+)(\d+)/Codes AB12 and CD345
Matches: “AB12”, “CD345”
Replace with `$1-$2` to insert a dash between the letters and the digits.
Practice tasks
Your regex runs over the text with the global flag. It passes when it finds every required match and nothing that should be left out.
Task 1: Write a pattern with three groups that matches ISO dates, ready for a `$3/$2/$1` replacement.
Dates: 2026-01-05, 05/01/2026, 2026-02-10
Task 2: Match `Last, First` name pairs so that `$2 $1` can flip them.
Khan, Ali and Test, Sara and solo
Task 3: Match dashed phone numbers in two captured parts so they can be reformatted. Undashed numbers must not match.
Call 0300-0000000 or 03000000000