1 var toObject = require('../internals/to-object');
3 var floor = Math.floor;
4 var replace = ''.replace;
5 var SUBSTITUTION_SYMBOLS = /\$([$&'`]|\d{1,2}|<[^>]*>)/g;
6 var SUBSTITUTION_SYMBOLS_NO_NAMED = /\$([$&'`]|\d{1,2})/g;
8 // https://tc39.es/ecma262/#sec-getsubstitution
9 module.exports = function (matched, str, position, captures, namedCaptures, replacement) {
10 var tailPos = position + matched.length;
11 var m = captures.length;
12 var symbols = SUBSTITUTION_SYMBOLS_NO_NAMED;
13 if (namedCaptures !== undefined) {
14 namedCaptures = toObject(namedCaptures);
15 symbols = SUBSTITUTION_SYMBOLS;
17 return replace.call(replacement, symbols, function (match, ch) {
19 switch (ch.charAt(0)) {
21 case '&': return matched;
22 case '`': return str.slice(0, position);
23 case "'": return str.slice(tailPos);
25 capture = namedCaptures[ch.slice(1, -1)];
29 if (n === 0) return match;
31 var f = floor(n / 10);
32 if (f === 0) return match;
33 if (f <= m) return captures[f - 1] === undefined ? ch.charAt(1) : captures[f - 1] + ch.charAt(1);
36 capture = captures[n - 1];
38 return capture === undefined ? '' : capture;