In a regex replacement, $1 and \1 both mean "the text captured by the first group". Which one you write depends on the tool: .NET, PowerShell, JavaScript and VS Code use $1; sed and Python use \1; Notepad++ accepts both. The whole match is $& (or $0 in .NET and Notepad++), a named group is ${name} in .NET and $<name> in JavaScript, and a literal dollar sign is $$. Inside the search pattern itself, a backreference is always \1.
This reference covers each form, which Windows tools support it, and the quoting and numbering traps that make a replacement come out wrong.
Backreferences in the pattern vs in the replacement
The two uses look similar but do different jobs:
- In the pattern,
\1means "the same text as group 1 matched, again, here".\b(\w+) \1\bfinds doubled words such as the the. This is the classic backreference. - In the replacement,
$1(or\1) inserts the text group 1 captured. Find(\w+), (\w+)and replace with$2 $1to turn Smith, John into John Smith.
Both rely on numbered groups. Groups are numbered by their opening parenthesis, counting from the left, and groups that start with (?: or a look-around do not count. If capture groups are new to you, start with capture groups in find and replace.
Replacement syntax by tool
| Insert | .NET / PowerShell | JavaScript | VS Code | Notepad++ | Python / sed |
|---|---|---|---|---|---|
| Group 1 | $1 | $1 | $1 | $1 or \1 | \1 |
| Group 1 before a digit | ${1} | $01 | $1 (see below) | ${1} | \g<1> (Python) |
| Named group | ${name} | $<name> | Not reliably supported | $+{name} | \g<name> (Python) |
| Whole match | $0 or $& | $& | $& or $0 | $0 or $& | \g<0> (Python), & (sed) |
| Text before the match | $` | $` | Not documented | $` | No |
| Text after the match | $' | $' | Not documented | $' | No |
| Literal dollar | $$ | $$ | $$ | \$ | $ |
| Change case of a group | No | No | \U$1, \L$1, \u$1, \l$1 | \U$1, \L$1 | No (sed GNU: yes) |
If a tool shows $1 literally in the output, either regex mode is off or it expects \1. Swap and try again.
The "group followed by a digit" trap
Suppose you capture a prefix and want to write 1.3 straight after it. $11.3 is ambiguous: group 1 followed by "1.3", or group 11 followed by ".3"? Engines resolve it differently:
- .NET and PowerShell use the longest group number that exists. With fewer than 11 groups,
$11becomes group 1 then "1". Writing${1}1.3removes the doubt, and is the form to use. - JavaScript also falls back to
$1when there is no group 11. You can write$011.3to force group 1, since two-digit forms are read first. - Notepad++ accepts
${1}.
Often the cleanest fix is to avoid the problem: move the fixed text out of the match with a lookbehind, so the replacement contains no group at all. Find (?<=version=")1\.2 and replace with 1.3. See lookahead and lookbehind examples for that technique.
Named groups
Names make long patterns readable. Define a group with (?<year>\d{4}) in the pattern (.NET, JavaScript, PCRE and Boost all accept this), then refer to it by name:
- .NET and PowerShell:
${year} - JavaScript:
$<year> - Notepad++:
$+{year}
Example in PowerShell: '2025-12-31' -replace '(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})', '${d}/${m}/${y}' returns 31/12/2025.
One .NET quirk to know: if you mix named and unnamed groups, .NET numbers the unnamed groups first and the named ones after them. In (?<y>\d{4})-(\d{2})-(\d{2}), .NET's $1 is the month, not the year, while JavaScript numbers all three from the left. Avoid the confusion by using names for every group, or none.
Inside the pattern, a named backreference is \k<name>: \b(?<w>\w+) \k<w>\b finds doubled words.
PowerShell quoting
PowerShell expands $ variables inside double-quoted strings before the regex engine sees them. So:
-replace '(\w+)@(\w+)', '$2 at $1'works.-replace '(\w+)@(\w+)', "$2 at $1"produces at , because$1and$2are empty PowerShell variables.- If you need double quotes, for example to insert a variable as well, escape the regex references with a backtick:
"`$1 at $domain".
PowerShell 6.1 and later also accept a script block as the replacement, for cases where you need logic rather than a template: 'Item 5' -replace '\d+', { [int]$_.Value * 2 } returns Item 10. Inside the block, $_ is the match object, so $_.Groups[1].Value gives group 1.
Literal dollars and backslashes
When the replacement must contain a real dollar sign, double it: in .NET and JavaScript, $$ inserts one $. To add a dollar sign in front of a captured price, the replacement is $$$1: two characters for the literal dollar, then $1. In Notepad++, write \$. Backslashes are literal in .NET and JavaScript replacements, but VS Code and Notepad++ treat them as escapes, so write \\ there. The full escaping rules are in escaping special characters in regex.
Common replacement recipes
| Task | Find | Replace |
|---|---|---|
| Swap two words | (\w+) (\w+) | $2 $1 |
| DD/MM/YYYY to YYYY-MM-DD | (\d{2})/(\d{2})/(\d{4}) | $3-$2-$1 |
| Wrap each match in tags | \bTODO\b | <strong>$&</strong> |
| Remove a doubled word | \b(\w+) \1\b | $1 |
| Keep a label, change its value | (color:\s*)red\b | $1blue |
Each recipe works as written in .NET, PowerShell, JavaScript, VS Code and Notepad++. In Notepad++ you may also write \1 in place of $1, and in Python or sed you must. Test any recipe on a few sample lines first, because real files rarely look exactly like the example.
More date conversions, including two-digit years and month names, are in changing date formats with regex.
Using backreferences across many files
Bulk Replace uses $1-style references in regex rules, for example $3-$2-$1 to reorder a date. Its before/after diff shows the replacement exactly as it will be written in every file, so an empty group or a group number in the wrong place is obvious before anything is saved. Each run is backed up, so if a reordered value turns out wrong, Bulk Replace restores every file in one click.
Frequently asked questions
What is the difference between $1 and \1?
Both refer to the first capture group. $1 is used in replacements by .NET, PowerShell and JavaScript, while \1 is the classic form used inside patterns and by tools like sed.
What does $& or $0 mean in a replacement?
Both insert the whole match in .NET and PowerShell. JavaScript and VS Code use $&.
How do I use a named group in the replacement?
Define it as (?<year>\d{4}) in the pattern and refer to it as ${year} in .NET and PowerShell, or $<year> in JavaScript.
