function replacer($0, bracketed, angled, dollarToken) {
                bracketed = bracketed || angled;

                // ES2018 added a new trailing `groups` arg that's passed to replacement functions
                // when the search regex uses native named capture
                const numNonCaptureArgs = isType(args[args.length - 1], 'Object') ? 4 : 3;
                const numCaptures = args.length - numNonCaptureArgs;

                // Handle named or numbered backreference with curly or angled braces: ${n}, $<n>
                if (bracketed) {
                    // Handle backreference to numbered capture, if `bracketed` is an integer. Use
                    // `0` for the entire match. Any number of leading zeros may be used.
                    if (/^\d+$/.test(bracketed)) {
                        // Type-convert and drop leading zeros
                        const n = +bracketed;
                        if (n <= numCaptures) {
                            return args[n] || '';
                        }
                    }

                    // Handle backreference to named capture. If the name does not refer to an
                    // existing capturing group, it's an error. Also handles the error for numbered
                    // backference that does not refer to an existing group.
                    // Using `indexOf` since having groups with the same name is already an error,
                    // otherwise would need `lastIndexOf`.
                    const n = captureNames ? captureNames.indexOf(bracketed) : -1;
                    if (n < 0) {
                        throw new SyntaxError(`Backreference to undefined group ${$0}`);
                    }
                    return args[n + 1] || '';
                }

                // Handle `$`-prefixed variable
                // Handle space/blank first because type conversion with `+` drops space padding
                // and converts spaces and empty strings to `0`
                if (dollarToken === '' || dollarToken === ' ') {
                    throw new SyntaxError(`Invalid token ${$0}`);
                }
                if (dollarToken === '&' || +dollarToken === 0) { // $&, $0 (not followed by 1-9), $00
                    return args[0];
                }
                if (dollarToken === '$') { // $$
                    return '$';
                }
                if (dollarToken === '`') { // $` (left context)
                    return args[args.length - 1].slice(0, args[args.length - 2]);
                }
                if (dollarToken === "'") { // $' (right context)
                    return args[args.length - 1].slice(args[args.length - 2] + args[0].length);
                }

                // Handle numbered backreference without braces
                // Type-convert and drop leading zero
                dollarToken = +dollarToken;
                // XRegExp behavior for `$n` and `$nn`:
                // - Backrefs end after 1 or 2 digits. Use `${..}` or `$<..>` for more digits.
                // - `$1` is an error if no capturing groups.
                // - `$10` is an error if less than 10 capturing groups. Use `${1}0` or `$<1>0`
                //   instead.
                // - `$01` is `$1` if at least one capturing group, else it's an error.
                // - `$0` (not followed by 1-9) and `$00` are the entire match.
                // Native behavior, for comparison:
                // - Backrefs end after 1 or 2 digits. Cannot reference capturing group 100+.
                // - `$1` is a literal `$1` if no capturing groups.
                // - `$10` is `$1` followed by a literal `0` if less than 10 capturing groups.
                // - `$01` is `$1` if at least one capturing group, else it's a literal `$01`.
                // - `$0` is a literal `$0`.
                if (!isNaN(dollarToken)) {
                    if (dollarToken > numCaptures) {
                        throw new SyntaxError(`Backreference to undefined group ${$0}`);
                    }
                    return args[dollarToken] || '';
                }

                // `$` followed by an unsupported char is an error, unlike native JS
                throw new SyntaxError(`Invalid token ${$0}`);
            }
Adds support for `${n}` (or `$<n>`) tokens for named and numbered backreferences in replacement
text, and provides named backreferences to replacement functions as `arguments[0].name`. Also
fixes browser bugs in replacement text syntax when performing a replacement using a nonregex
search value, and the value of a replacement regex's `lastIndex` property during replacement
iterations and upon completion. Note that this doesn't support SpiderMonkey's proprietary third
(`flags`) argument. Use via `XRegExp.replace`.

@memberOf String
@param {RegExp|String} search Search pattern to be replaced.
@param {String|Function} replacement Replacement string or a function invoked to create it.
@returns {string} New string with one or all matches replaced.