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

Method getReplacement

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 2

replaceMethod · 0.45
sliceMethod · 0.45

Tested by

no test coverage detected