| Replacement Text Tutorial |
| Introduction |
| Characters |
| Non-Printable Characters |
| Matched Text |
| Backreferences |
| Match Context |
| Case Conversion |
| Conditionals |
| Control Verb Argument |
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.
In the replacement text, $` (dollar backtick) is substituted with the part of the subject string to the left of the regex match in the JGsoft applications, Delphi, .NET, JavaScript, VBScript, Boost, and std::regex. It is also the variable that holds the part of the subject string to the left of the regex match in Perl. \` (backslash backtick) works in the JGsoft applications, Delphi, and Ruby.
In the same applications, you can use $' (dollar quote) or \' (backslash quote) to insert the part of the subject string to the right of the regex match.
In the replacement text, $_ is substituted with the entire subject string in the JGsoft applications, Delphi, and .NET. In Perl, $_ is the default variable that the regex is applied to if you use a regular expression without the matching operator =~. \_ is just an escaped underscore. It has no special meaning in any application.
Boost 1.42 added some alternative syntax of its own invention. $PREMATCH and ${^PREMATCH} are synonyms for $`. $POSTMATCH and ${^POSTMATCH} are synonyms for $'.
If you find the content on this website helpful they you may want a copy you can read offline or even print, or browse the site as often as you want without ads. You can purchase your own copy of the Regular-Expressions.info printable PDF download. As a bonus, you'll get a lifetime of advertisement-free access to this site!
| 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/replacecontext.html
Page last updated: 18 June 2025
Site last updated: 09 January 2026
Copyright © 2003-2026 Jan Goyvaerts. All rights reserved.