{"record":{"id":"6c82789c5a5b69bc","repo":"rolldown/rolldown","slug":"magicstring-prototype-replaceall-called-with-a-non","errorCode":null,"errorMessage":"MagicString.prototype.replaceAll called with a non-global RegExp argument","messagePattern":"MagicString\\.prototype\\.replaceAll called with a non-global RegExp argument","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/rolldown/src/binding-magic-string.ts","lineNumber":130,"sourceCode":"  // - Non-global sticky: advance to match end, or reset to 0 on miss\n  // - Non-global non-sticky: lastIndex is not modified by .replace()\n  if (searchValue.global) {\n    searchValue.lastIndex = 0;\n  } else if (searchValue.sticky) {\n    searchValue.lastIndex = lastMatchEnd === -1 ? 0 : lastMatchEnd;\n  }\n  return this;\n};\n\nNativeBindingMagicString.prototype.replaceAll = function (\n  searchValue: string | RegExp,\n  replacement: string,\n): any {\n  if (typeof searchValue === 'string') {\n    return nativeReplaceAll.call(this, searchValue, replacement);\n  }\n  if (!searchValue.global) {\n    throw new TypeError(\n      'MagicString.prototype.replaceAll called with a non-global RegExp argument',\n    );\n  }\n  searchValue.lastIndex = 0;\n  (this as any).replaceRegex(searchValue, replacement);\n  searchValue.lastIndex = 0;\n  return this;\n};\n\nexport interface RolldownMagicString extends NativeBindingMagicString {\n  readonly isRolldownMagicString: true;\n  /** Accepts a string or RegExp pattern. RegExp supports `$&`, `$$`, and `$N` substitutions. */\n  replace(from: string | RegExp, to: string): this;\n  /** Accepts a string or RegExp pattern. RegExp must have the global (`g`) flag. */\n  replaceAll(from: string | RegExp, to: string): this;\n}\n\ntype RolldownMagicStringConstructor = Omit<typeof NativeBindingMagicString, 'prototype'> & {","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/rolldown/rolldown/blob/91b44b9d7bcfb5725533478e044e3891a251b2c5/packages/rolldown/src/binding-magic-string.ts#L112-L148","documentation":"binding-magic-string.ts implements replaceAll to match the JS magic-string/String.prototype.replaceAll spec: a RegExp argument must have the global (g) flag. If a non-global RegExp is passed, it throws this TypeError instead of silently replacing only the first match like the native binding would.","triggerScenarios":"Calling magicString.replaceAll(/pattern/, replacement) where /pattern/ lacks the `g` flag — `if (!searchValue.global) throw new TypeError(...)` fires.","commonSituations":"Porting code from replace() to replaceAll() without adding the g flag; constructing a RegExp from a string pattern and forgetting { flags: 'g' }; copying regexes used with String.replace into replaceAll.","solutions":["Add the g flag to the RegExp (e.g. /pattern/g)","If you only want one replacement, call replace() instead of replaceAll()","When building RegExp dynamically, pass 'g' in the flags argument: new RegExp(pattern, 'g')"],"exampleFix":"// before\ns.replaceAll(/foo/, 'bar'); // TypeError\n// after\ns.replaceAll(/foo/g, 'bar');","handlingStrategy":"validation","validationCode":"function assertGlobalRegExp(re) {\n  if (re instanceof RegExp && !re.global) throw new TypeError('replaceAll requires a global RegExp (g flag)');\n}","typeGuard":"function isGlobalRegExp(v: unknown): v is RegExp & { global: true } {\n  return v instanceof RegExp && v.global;\n}","tryCatchPattern":"try {\n  s.replaceAll(searchValue, replacement);\n} catch (e) {\n  if (e instanceof TypeError && /non-global RegExp/.test(e.message)) {\n    s.replaceAll(new RegExp(searchValue.source, 'g'), replacement);\n  } else throw e;\n}","preventionTips":["Always write regexes for replaceAll with the g flag","Prefer string search values over RegExp when exact-literal replacement is intended","Add a lint rule (require-unicode-regexp style) or unit test covering replaceAll regex usage"],"tags":["typeerror","regexp","magic-string","replaceall"],"backgroundTag":"invalid-regex-pattern","analyzedSha":"91b44b9d7bcfb5725533478e044e3891a251b2c5","analyzedAt":"2026-09-07T16:04:13.504Z","contentChangedAt":"2026-09-07T16:04:13.504Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}