See what a regex replacement will do before you run it.
Every $1 and $& in the replacement template is
spelled out one at a time, and
each piece of the result is traced back to the token that produced it.
JavaScript replacement strings have several forms that
quietly turn into something else without raising an error.
Naming those is the point of this page.
$1, there is no error — the characters $1
are emitted verbatim. This is what bites you after deleting one capture group.$<name> is only special if the pattern has at least one named group.
Given that, a misspelled name is not an error — it becomes the empty string and disappears.
In a pattern with no named groups at all, $<name> comes out as plain text instead.\1 is not a back-reference. In a JavaScript replacement string it is
just 1 (the \ is consumed by the string literal).
This is the habit that carries over from sed and Python.${1} is not special either. It comes out as ${1}.$` and $' mean the text before and after the match, but
both refer to the original string — not to the half-replaced one.g, only the first match is replaced. No error is raised.x* and friends) land between characters:
"abc".replace(/x*/g, "-") gives -a-b-c-.String.prototype.replace only hands back the finished string, so tracing
each character to the token that produced it means assembling the result yourself.replace and the outcome is shown in
the self-check box. If they disagree, the browser is the one to trust.