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

Method getReplacement

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

Source from the content-addressed store, hash-verified

1205 /** @internal */
1206 _replaceRegexp(searchValue: RegExp, replacement: string | ReplacementFunction): this {
1207 function getReplacement(match: RegExpMatchArray, str: string): string {
1208 if (typeof replacement === 'string') {
1209 return replacement.replace(/\$(\$|&|\d+)/g, (_: string, i: string) => {
1210 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_a_parameter
1211 if (i === '$')
1212 return '$'
1213 if (i === '&')
1214 return match[0]
1215 const num = +i
1216 if (num < match.length)
1217 return match[+i]
1218 return `$${i}`
1219 })
1220 }
1221 else {
1222 return replacement(match[0], ...match.slice(1), match.index, str, match.groups)
1223 }
1224 }
1225 const replaceMatch = (match: RegExpMatchArray): void => {
1226 if (match.index == null)
1227 return

Callers

nothing calls this directly

Calls 2

replaceMethod · 0.45
sliceMethod · 0.45

Tested by

no test coverage detected