Replacement Strings Reference: Matched Text and Backreferences (original) (raw)
Feature
Syntax
Description
Example
\&
Insert a literal ampersand.
Replacing with \& yields &
no
no
YES
YES
extended
no
no
YES
no
no
no
no
no
sed
YES
YES
no
error
\&
Insert the whole regex match.
Replacing \d+ with [\&] in 1a2b yields [1]a[2]b
YES
no
no
no
no
no
YES
no
no
no
no
no
YES
no
no
no
no
error
$&
Insert the whole regex match.
Replacing \d+ with [$&] in 1a2b yields [1]a[2]b
YES
YES
error
YES
error
no
YES
no
YES
YES
YES
no
no
default
all
default
no
no
error
&
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
sed
sed
YES
no
no
\0
Insert the whole regex match.
Replacing \d+ with [\0] in 1a2b yields [1]a[2]b
YES
no
no
no
no
YES
YES
no
no
no
no
no
YES
sed
sed
YES
no
error
$0
Insert the whole regex match.
Replacing \d+ with [$0] in 1a2b yields [1]a[2]b
YES
YES
YES
error
YES
YES
YES
no
no
no
YES
no
no
default
VC’08–VC’13
all
default
no
no
YES
\g<0>
Insert the whole regex match.
Replacing \d+ with [\g<0>] in 1a2b yields [1]a[2]b
YES
no
no
no
no
no
no
no
no
no
no
YES
no
no
no
no
no
error
Whole match MATCHandMATCH and MATCHand{^MATCH}
Insert the whole regex match.
Replacing \d+ with [$MATCH] in 1a2b yields [1]a[2]b
no
no
error
error
error
no
no
no
no
no
error
no
no
no
all
default
1.42–1.88
no
no
error
\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
no
YES
no
YES
YES
YES
no
no
no
YES
YES
sed
YES
YES
YES
error
\10 through \99
Insert the text matched by capturing groups 10 through 99.
YES
no
no
no
no
YES
YES
no
no
no
no
YES
no
no
no
no
no
no
\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
n/a
n/a
n/a
n/a
no
YES
n/a
n/a
n/a
n/a
no
n/a
n/a
n/a
n/a
n/a
n/a
Backreference 1through1 through 1through9
Insert the text matched by one of the first 9 capturing groups.
Replacing (a)(b)(c) with 3333$1 in abc yields cca
YES
YES
YES
YES
YES
YES
YES
no
YES
YES
YES
no
no
default
all
default
no
no
YES
Backreference 10through10 through 10through99
Insert the text matched by capturing groups 10 through 99.
YES
YES
YES
YES
YES
YES
YES
no
YES
YES
YES
no
no
default
all
default
no
no
YES
Backreference and literal 10through10 through 10through99
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 39393938$17 in abc yields c9c8a7
YES
ECMA
YES
no
no
no
YES
n/a
YES
YES
no
n/a
n/a
no
no
n/a
n/a
YES
Backreference 1through{1} through 1through{99}
Insert the text matched by capturing groups 1 through 99.
Replacing (a)(b)(c) with 3{3}3{3}${1} in abc yields cca
YES
YES
error
YES
YES
YES
YES
no
no
no
YES
no
no
no
all
default
no
no
error
\g<1> through \g<99>
Insert the text matched by capturing groups 1 through 99.
Replacing (a)(b)(c) with \g<3>\g<3>\g<1> in abc yields cca
YES
no
no
no
no
no
no
no
no
no
no
YES
no
no
no
no
no
error
${name}
Insert the text matched by the named capturing group “name”.
Replacing (?'one'a)(?'two'b) with two{two}two{one} in ab yields ba
YES
YES
7
error
YES
no
YES
no
no
no
YES
no
no
no
no
no
no
error
$+{name}
Insert the text matched by the named capturing group “name”.
Replacing (?'one'a)(?'two'b) with +two+{two}+two+{one} in ab yields ba
no
no
error
5.10
error
no
no
no
no
no
error
no
no
no
all
default
1.42–1.88
no
no
error
$name
Insert the text matched by the named capturing group “name”.
Replacing (?'one'a)(?'two'b) with twotwotwoone in ab yields ba
no
no
error
error
YES
no
no
no
no
no
error
no
no
no
no
no
no
error
\g
Insert the text matched by the named capturing group “name”.
Replacing (?Pa)(?Pb) with \g\g in ab yields ba
YES
no
no
no
no
no
YES
no
no
no
no
YES
no
no
no
no
no
error
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
no
error
YES
error
YES
YES
YES
no
no
error
error
YES
default VC’08–VC’22
sed VC’15–VC’22
YES
YES
YES
YES
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
YES
error
no
error
no
no
no
YES
YES
error
error
no
sed
VC’08–VC’13 error
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
YES
YES
YES
error
YES
YES
YES
YES
YES
YES
3.5
YES
YES
YES
YES
YES
YES
\+
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
YES
no
no
no
no
no
YES
no
no
no
no
error
\+
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
no
no
no
no
no
no
no
no
no
error
$+
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
error
5.18
error
no
YES
no
no
no
error
no
no
no
no
no
no
error
$+
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
YES
error
5.8–5.16
error
no
no
no
no
YES
error
no
no
no
all
default
1.42–1.88
no
no
error
$^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
error
YES
error
no
no
no
no
no
error
no
no
no
all
default
1.42–1.88
no
no
error
Last backreference LASTSUBMATCHRESULTandLAST_SUBMATCH_RESULT and LASTSUBMATCHRESULTand{^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
error
error
error
no
no
no
no
no
error
no
no
no
all
default
1.42–1.88
no
no
error
Last backreference LASTPARENMATCHandLAST_PAREN_MATCH and LASTPARENMATCHand{^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
error
error
error
no
no
no
no
no
error
no
no
no
all
default
1.42–1.88
no
no
error
Feature
Syntax
Description
Example