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

Method getReplacement

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

Source from the content-addressed store, hash-verified

1133 /** @internal */
1134 _replaceRegexp(searchValue: RegExp, replacement: string | ReplacementFunction): this {
1135 function getReplacement(match: RegExpMatchArray, str: string): string {
1136 if (typeof replacement === 'string') {
1137 return replacement.replace(/\$(\$|&|\d+)/g, (_: string, i: string) => {
1138 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_a_parameter
1139 if (i === '$')
1140 return '$'
1141 if (i === '&')
1142 return match[0]
1143 const num = +i
1144 if (num < match.length)
1145 return match[+i]
1146 return `$${i}`
1147 })
1148 }
1149 else {
1150 return replacement(match[0], ...match.slice(1), match.index, str, match.groups)
1151 }
1152 }
1153 function matchAll(re: RegExp, str: string): RegExpExecArray[] {
1154 const matches = []
1155 while (true) {

Callers

nothing calls this directly

Calls 2

replaceMethod · 0.45
sliceMethod · 0.45

Tested by

no test coverage detected