| Replacement Reference |
| Introduction |
| Table of Contents |
| Characters |
| Matched Text & Backreferences |
| Case Conversion |
| Context |
| Conditionals |
| Regular Expressions Reference |
| Feature | Syntax | Description | Example | JGsoft | Python | JavaScript | VBScript | XRegExp | .NET | Java | ICU | RE2 | Perl | PCRE2 | PHP | Delphi | R | Ruby | std::regex | Boost | Tcl | Oracle | XPath |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Ampersand | \& | Insert a literal ampersand. | Replacing with \& yields & | no | no | no | no | no | no | YES | YES | error | YES | extended | no | no | YES | no | sed | YES | YES | no | error |
| Whole match | \& | Insert the whole regex match. | Replacing \d+ with [\&] in 1a2b yields [1]a[2]b | YES | no | no | no | no | no | no | no | error | no | no | no | YES | no | YES | no | no | no | no | error |
| Whole match | $& | Insert the whole regex match. | Replacing \d+ with [$&] in 1a2b yields [1]a[2]b | YES | no | YES | YES | YES | YES | error | error | no | YES | 10.45 | no | YES | no | no | default | all default | no | no | error |
| Whole match | & | Insert the whole regex match. | Replacing \d+ with [&] in 1a2b yields [1]a[2]b | no | no | no | no | no | no | no | no | no | no | no | no | no | no | no | sed | sed | YES | no | no |
| Whole match | \0 | Insert the whole regex match. | Replacing \d+ with [\0] in 1a2b yields [1]a[2]b | YES | no | no | no | no | no | no | no | YES | no | no | YES | YES | no | YES | sed | sed | YES | no | error |
| Whole match | $0 | Insert the whole regex match. | Replacing \d+ with [$0] in 1a2b yields [1]a[2]b | YES | no | no | no | YES | YES | YES | YES | no | error | YES | YES | YES | no | no | default | all default | no | no | YES |
| Whole match | ${0} | Insert the whole regex match. | Replacing \d+ with [${0}] in 1a2b yields [1]a[2]b | YES | no | no | no | YES | YES | error | error | no | error | YES | YES | YES | no | no | no | all default | no | no | error |
| Whole match | \g<0> | Insert the whole regex match. | Replacing \d+ with [\g<0>] in 1a2b yields [1]a[2]b | YES | YES | no | no | no | no | no | no | error | no | extended 10.45 | no | no | no | no | no | no | no | no | error |
| Whole match | $MATCH and ${^MATCH} | Insert the whole regex match. | Replacing \d+ with [$MATCH] in 1a2b yields [1]a[2]b | no | no | no | no | error | no | error | error | no | error | error | no | no | no | no | no | all default 1.42 | no | no | error |
| Backreference | \1 through \9 | Insert the text matched by one of the first 9 capturing groups. | Replacing (a)(b)(c) with \3\3\1 in abc yields cca | YES | YES | no | no | no | no | no | no | YES | YES | extended 10.45 | YES | YES | YES | YES | sed | YES | YES | YES | error |
| Backreference | \10 through \99 | Insert the text matched by capturing groups 10 through 99. | YES | YES | no | no | no | no | no | no | no | no | extended 10.45 | YES | YES | no | no | no | no | no | no | error | |
| Backreference and literal | \10 through \99 | When there are fewer capturing groups than the 2-digit number, treat this as a single-digit backreference followed by a literal number instead of as an invalid backreference. | Replacing (a)(b)(c) with \39\38\17 in abc yields c9c8a7 | YES | no | n/a | n/a | n/a | n/a | n/a | n/a | n/a | n/a | no | no | YES | n/a | n/a | n/a | n/a | n/a | n/a | n/a |
| Backreference | \100 through \999 | Insert the text matched by capturing groups 100 through 999. | no | no | no | no | no | no | no | no | no | no | extended 10.45 | no | no | no | no | no | no | no | no | error | |
| Backreference and literal | \100 through \999 | When there are fewer capturing groups than the 3-digit number, treat this as a 2-digit backreference followed by a literal number instead of as an invalid backreference. | Replacing (a)(b)(c) with \345\234\123 in abc yields c45b34a23 | n/a | n/a | n/a | n/a | n/a | n/a | n/a | n/a | n/a | n/a | no | n/a | n/a | n/a | n/a | n/a | n/a | n/a | n/a | n/a |
| Backreference | $1 through $9 | Insert the text matched by one of the first 9 capturing groups. | Replacing (a)(b)(c) with $3$3$1 in abc yields cca | YES | no | YES | YES | YES | YES | YES | YES | no | YES | YES | YES | YES | no | no | default | all default | no | no | YES |
| Backreference | $10 through $99 | Insert the text matched by capturing groups 10 through 99. | YES | no | YES | YES | YES | YES | YES | YES | no | YES | YES | YES | YES | no | no | default | all default | no | no | YES | |
| Backreference and literal | $10 through $99 | When there are fewer capturing groups than the 2-digit number, treat this as a single-digit backreference followed by a literal number instead of as an invalid backreference. | Replacing (a)(b)(c) with $345$234$123 in abc yields c45b34a23 | YES | n/a | YES | YES | no | ECMA | YES | YES | n/a | no | no | no | YES | n/a | n/a | no | no | n/a | n/a | YES |
| Backreference | $100 through $999 | Insert the text matched by capturing groups 100 through 999. | no | no | no | no | no | YES | YES | YES | no | YES | YES | no | no | no | no | no | all default 1.42 | no | no | YES | |
| Backreference and literal | $100 through $999 | When there are fewer capturing groups than the 3-digit number, treat this as a 2-digit backreference followed by a literal number instead of as an invalid backreference. | Replacing (a)(b)(c) with $39$38$17 in abc yields c9c8a7 | n/a | n/a | n/a | n/a | n/a | ECMA | YES | YES | n/a | no | no | n/a | n/a | n/a | n/a | n/a | no | n/a | n/a | YES |
| Backreference | ${1} through ${999} | Insert the text matched by capturing groups 1 through 999. | Replacing (a)(b)(c) with ${3}${3}${1} in abc yields cca | YES | no | no | no | YES | YES | error | error | no | YES | YES | YES | YES | no | no | no | all default | no | no | error |
| Backreference | \g<1> through \g<999> | Insert the text matched by capturing groups 1 through 999. | Replacing (a)(b)(c) with \g<3>\g<3>\g<1> in abc yields cca | YES | YES | no | no | no | no | no | no | error | no | extended 10.45 | no | no | no | no | no | no | no | no | error |
| Named backreference | ${name} | Insert the text matched by the named capturing group “name”. | Replacing (?'one'a)(?'two'b) with ${two}${one} in ab yields ba | YES | no | no | no | YES | YES | 7 4 error | YES | no | error | YES | no | YES | no | no | no | no | no | no | error |
| Named backreference | $+{name} | Insert the text matched by the named capturing group “name”. | Replacing (?'one'a)(?'two'b) with $+{two}$+{one} in ab yields ba | no | no | no | no | error | no | error | error | no | YES | error | no | no | no | no | no | all default 1.42 | no | no | error |
| Named backreference | $name | Insert the text matched by the named capturing group “name”. | Replacing (?'one'a)(?'two'b) with $two$one in ab yields ba | no | no | no | no | error | no | error | error | no | error | YES | no | no | no | no | no | no | no | no | error |
| Named backreference | \g<name> | Insert the text matched by the named capturing group “name”. | Replacing (?P<one>a)(?P<two>b) with \g<two>\g<one> in ab yields ba | YES | YES | no | no | no | no | no | no | error | no | extended 10.45 | no | YES | no | no | no | no | no | no | error |
| Invalid backreference | Any supported backreference syntax | A backreference that indicates a number greater than the highest-numbered group or a name of a group that does not exist is replaced with the empty string. | V1 only | error | no | no | error | no | error | error | YES | error | YES | YES | YES | YES | default VC’10 sed VC’15 | YES | YES | no | YES | ||
| Invalid backreference | Any supported backreference syntax | A backreference that indicates a number greater than the highest-numbered group or a name of a group that does not exist is treated as literal text that is inserted as such in the replacement. | V2 error | error | YES | YES | error | YES | error | error | no | no | error | no | no | no | no | sed | no | no | YES | no | |
| Invalid backreference | Any supported backreference syntax | No replacements are made at all if the replacement string contains a backreference that indicates a number greater than the highest-numbered group or a name of a group that does not exist. | V2 error | error | no | no | error | no | error | error | 2020/06 | no | error | no | no | no | no | sed | no | no | no | no | |
| Backreference to non-participating group | Any supported backreference syntax | A backreference to a non-participating capturing group is replaced with the empty string. | YES | 3.5 2.4 error | YES | YES | YES | YES | YES | YES | YES | YES | error | YES | YES | YES | YES | YES | YES | YES | YES | YES | |
| Last backreference | \+ | Insert the text matched by the highest-numbered capturing group that actually participated in the match. | Replacing (a)(z)? with [\+] in ab yields [a]b | YES | no | no | no | no | no | no | no | error | no | no | no | YES | no | YES | no | no | no | no | error |
| Last backreference | \+ | Insert the text matched by the highest-numbered capturing group, regardless of whether it participated in the match. | Replacing (a)(z)? with [\+] in ab yields []b | no | no | no | no | no | no | no | no | error | no | no | no | no | no | no | no | no | no | no | error |
| Last backreference | $+ | Insert the text matched by the highest-numbered capturing group that actually participated in the match. | Replacing (a)(z)? with [$+] in ab yields [a]b | YES | no | no | no | error | no | error | error | no | 5.18 | error | no | YES | no | no | no | no | no | no | error |
| Last backreference | $+ | Insert the text matched by the highest-numbered capturing group, regardless of whether it participated in the match. | Replacing (a)(z)? with [$+] in ab yields []b | no | no | no | YES | error | YES | error | error | no | error | no | no | no | no | no | all default 1.42 | no | no | error | |
| Last backreference | $^N | Insert the text matched by the highest-numbered capturing group that actually participated in the match. | Replacing (a)(z)? with [$^N] in ab yields [a]b | no | no | no | no | error | no | error | error | no | YES | error | no | no | no | no | no | all default 1.42 | no | no | error |
| Last backreference | $LAST_SUBMATCH_RESULT and ${^LAST_SUBMATCH_RESULT} | Insert the text matched by the highest-numbered capturing group that actually participated in the match. | Replacing (a)(z)? with [$LAST_SUBMATCH_RESULT] in ab yields [a]b | no | no | no | no | error | no | error | error | no | error | error | no | no | no | no | no | all default 1.42 | no | no | error |
| Last backreference | $LAST_PAREN_MATCH and ${^LAST_PAREN_MATCH} | Insert the text matched by the highest-numbered capturing group, regardless of whether it participated in the match. | Replacing (a)(z)? with [$LAST_PAREN_MATCH] in ab yields []b | no | no | no | no | error | no | error | error | no | error | error | no | no | no | no | no | all default 1.42 | no | no | error |
| Feature | Syntax | Description | Example | JGsoft | Python | JavaScript | VBScript | XRegExp | .NET | Java | ICU | RE2 | Perl | PCRE2 | PHP | Delphi | R | Ruby | std::regex | Boost | Tcl | Oracle | XPath |
| Quick Start | Tutorial | Search & Replace | Tools & Languages | Examples | Reference |
| Introduction | Table of Contents | Characters | Matched Text & Backreferences | Case Conversion | Context | Conditionals | Regular Expressions Reference |
Page URL: https://www.regular-expressions.info/refreplacebackref.html
Page last updated: 9 June 2025
Site last updated: 09 January 2026
Copyright © 2003-2026 Jan Goyvaerts. All rights reserved.