@@ -1556,6 +1556,12 @@ fixed.replace = function(search, replacement) {
15561556
15571557 function replacer ( $0 , bracketed , angled , dollarToken ) {
15581558 bracketed = bracketed || angled ;
1559+
1560+ // ES2018 added a new trailing `groups` arg that's passed to replacement functions
1561+ // when the search regex uses native named capture
1562+ const numNonCaptureArgs = isType ( args [ args . length - 1 ] , 'Object' ) ? 4 : 3 ;
1563+ const numCaptures = args . length - numNonCaptureArgs ;
1564+
15591565 // Named or numbered backreference with curly or angled braces
15601566 if ( bracketed ) {
15611567 // XRegExp behavior for `${n}` or `$<n>`:
@@ -1568,7 +1574,7 @@ fixed.replace = function(search, replacement) {
15681574 // 3. If the name or number does not refer to an existing capturing group, it's
15691575 // an error.
15701576 let n = + bracketed ; // Type-convert; drop leading zeros
1571- if ( n <= args . length - 3 ) {
1577+ if ( n <= numCaptures ) {
15721578 return args [ n ] || '' ;
15731579 }
15741580 // Groups with the same name is an error, else would need `lastIndexOf`
@@ -1607,7 +1613,7 @@ fixed.replace = function(search, replacement) {
16071613 // - `$01` is `$1` if at least one capturing group, else it's a literal `$01`.
16081614 // - `$0` is a literal `$0`.
16091615 if ( ! isNaN ( dollarToken ) ) {
1610- if ( dollarToken > args . length - 3 ) {
1616+ if ( dollarToken > numCaptures ) {
16111617 throw new SyntaxError ( `Backreference to undefined group ${ $0 } ` ) ;
16121618 }
16131619 return args [ dollarToken ] || '' ;
0 commit comments