| Replacement Text Tutorial |
| Introduction |
| Characters |
| Non-Printable Characters |
| Matched Text |
| Backreferences |
| Match Context |
| Case Conversion |
| Conditionals |
| Control Verb Argument |
A replacement string, also known as the replacement text, is the text that each regular expression match is replaced with during a search-and-replace. In most applications, the replacement text supports special syntax that allows you to reuse the text matched by the regular expression or parts thereof in the replacement. This tutorial explains this syntax. While replacement strings are fairly simple compared with regular expressions, there is still great variety between the syntax used by various applications and their actual behavior.
On this website, replacement strings are shown as replace like you would enter them in the Replace box of an application. Literal text in the replacement is highlighted in gray. As $&\$ shows, special tokens are highlighted in blue with bold text for backreferences, and escaped characters in gray with blue text.
Literal Characters and Special Characters
The simplest replacement text consists of only literal characters. Certain characters have special meanings in replacement strings and have to be escaped. Escaping rules may get a bit complicated when using replacement strings in software source code.
Non-printable characters such as control characters and special spacing or line break characters are easier to enter using control character escapes or hexadecimal escapes.
Reinserting the entire regex match into the replacement text allows a search-and-replace to insert text before and after regular expression matches without really replacing anything.
Backreferences to named and numbered capturing groups in the regular expression allow the replacement text to reuse parts of the text matched by the regular expression.
Some applications support special tokens in replacement strings that allow you to insert the subject string or the part of the subject string before or after the regex match. This can be useful when the replacement text syntax is used to collect search matches and their context instead of making replacements in the subject string.
Some applications can insert the text matched by the regex or by capturing groups converted to uppercase or lowercase.
Some applications can use one replacement or another replacement depending on whether a capturing group participated in the match. This allows you to use different replacements for different matches of the regular expression.
Backtracking Control Verb Arguments
Insert the argument of the last backtracking control verb. Requires a deep understanding of how the regular expression engine works.
| Quick Start | Tutorial | Search & Replace | Tools & Languages | Examples | Reference |
| Introduction | Characters | Non-Printable Characters | Matched Text | Backreferences | Match Context | Case Conversion | Conditionals | Control Verb Argument |
Page URL: https://www.regular-expressions.info/replacetutorial.html
Page last updated: 18 June 2025
Site last updated: 09 January 2026
Copyright © 2003-2026 Jan Goyvaerts. All rights reserved.