MCPcopy Create free account
hub / github.com/Rich-Harris/magic-string / getReplacement

Method getReplacement

src/MagicString.ts:1119–1136  ·  view source on GitHub ↗
(match: RegExpMatchArray, str: string)

Source from the content-addressed store, hash-verified

1117 /** @internal */
1118 _replaceRegexp(searchValue: RegExp, replacement: string | ReplacementFunction): this {
1119 function getReplacement(match: RegExpMatchArray, str: string): string {
1120 if (typeof replacement === 'string') {
1121 return replacement.replace(/\$(\$|&|\d+)/g, (_: string, i: string) => {
1122 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_a_parameter
1123 if (i === '$')
1124 return '$'
1125 if (i === '&')
1126 return match[0]
1127 const num = +i
1128 if (num < match.length)
1129 return match[+i]
1130 return `$${i}`
1131 })
1132 }
1133 else {
1134 return replacement(match[0], ...match.slice(1), match.index, str, match.groups)
1135 }
1136 }
1137 function matchAll(re: RegExp, str: string): RegExpExecArray[] {
1138 const matches = []
1139 while (true) {

Callers

nothing calls this directly

Calls 2

replaceMethod · 0.45
sliceMethod · 0.45

Tested by

no test coverage detected