javascript – Regex for matching a previous group in the pattern?

In a regex pattern, a backreference to the first capturing group is always \1, not $1.

Reason: $ means “end of string” (or end of line, depending on context) in a regex.

In a replace pattern (which isn’t a regex), some dialects allow $1 (e. g. .NET, Java, Perl and JavaScript), some allow \1 (Python and Ruby), and some allow both (PHP and JGSoft).

Edit: Since you wrote that you couldn’t find any documentation on this, check out these overviews on regular-expressions.info:

Read more here: Source link