{"record":{"id":"99be27de2bc52f0d","repo":"different-ai/openwork","slug":"string-replaceall-requires-a-regular-expression-wi","errorCode":null,"errorMessage":"String.replaceAll requires a regular expression with the global (g) flag: write /${pattern.source}/${pattern.flags}g, or use String.replace to replace only the first match.","messagePattern":"String\\.replaceAll requires a regular expression with the global \\(g\\) flag: write /(.+?)/(.+?)g, or use String\\.replace to replace only the first match\\.","errorType":"exception","errorClass":"InterpreterRuntimeError","httpStatus":null,"severity":"error","filePath":"packages/codemode/src/interpreter/runtime.ts","lineNumber":426,"sourceCode":"    case \"startsWith\":\n      result = value.startsWith(str(0), optNum(1))\n      break\n    case \"endsWith\":\n      result = value.endsWith(str(0), optNum(1))\n      break\n    case \"indexOf\":\n      result = value.indexOf(str(0), optNum(1))\n      break\n    case \"lastIndexOf\":\n      result = value.lastIndexOf(str(0), optNum(1))\n      break\n    case \"replace\":\n    case \"replaceAll\": {\n      if (args[0] instanceof SandboxRegExp) {\n        const pattern = (args[0] as SandboxRegExp).regex\n        const replacement = str(1)\n        if (name === \"replaceAll\" && !pattern.global) {\n          throw new InterpreterRuntimeError(\n            `String.replaceAll requires a regular expression with the global (g) flag: write /${pattern.source}/${pattern.flags}g, or use String.replace to replace only the first match.`,\n            node,\n          )\n        }\n        result = name === \"replace\" ? value.replace(pattern, replacement) : value.replaceAll(pattern, replacement)\n        break\n      }\n      if (name === \"replace\") {\n        result = value.replace(str(0), str(1))\n        break\n      }\n      result = value.replaceAll(str(0), str(1))\n      break\n    }\n    case \"match\": {\n      const pattern = toHostRegex(args[0], name, node)\n      const matched = value.match(pattern)\n      if (matched === null) return null","sourceCodeStart":408,"sourceCodeEnd":444,"githubUrl":"https://github.com/different-ai/openwork/blob/2b7df46e8ae1517d64c896c7793d2d52ec845669/packages/codemode/src/interpreter/runtime.ts#L408-L444","documentation":"In this sandbox String.replaceAll with a RegExp pattern requires the global (g) flag, mirroring the host JavaScript spec where replaceAll with a non-global regex throws a TypeError. The library throws proactively with an actionable message that prints the corrected regex literal (source + flags + g) and points to String.replace for first-match-only replacement.","triggerScenarios":"Calling str.replaceAll(/foo/, 'bar') where the regex lacks the g flag; building RegExp dynamically without including 'g' in the flags string; reusing a regex constant written for String.replace and passing it to replaceAll.","commonSituations":"Refactoring replace() calls to replaceAll() without updating flags; constructing new RegExp(userPattern) with default empty flags; sharing regexes between match/replace/replaceAll call sites with differing flag needs.","solutions":["Add the g flag: str.replaceAll(/foo/g, 'bar')","When constructing dynamically, include 'g' in flags: new RegExp(p, flags + 'g')","Use String.replace(pattern, replacement) instead if only the first match should be replaced","Use a plain string as the search value — replaceAll accepts strings without a g flag"],"exampleFix":"// before\ntext.replaceAll(/\\s+/, \"_\")\n// after\ntext.replaceAll(/\\s+/g, \"_\")\n// or first-match only:\ntext.replace(/\\s+/, \"_\")","handlingStrategy":"validation","validationCode":"function globalize(re) { return re instanceof RegExp && !re.global ? new RegExp(re.source, re.flags + \"g\") : re }\ntext.replaceAll(globalize(pattern), replacement)","typeGuard":"const isGlobalRegex = (v) => v instanceof RegExp && v.global","tryCatchPattern":"try { out = text.replaceAll(re, rep) } catch (e) { if (String(e.message).includes(\"global (g) flag\")) out = text.replaceAll(new RegExp(re.source, re.flags + \"g\"), rep); else throw e }","preventionTips":["Always write replaceAll patterns with the g flag","Use String.replace when only the first match should change","Centralize regex construction and inject 'g' for global use cases","Prefer plain-string search values where possible"],"tags":["codemode","interpreter","regex","string-methods","replaceall"],"backgroundTag":"regex-missing-global-flag","analyzedSha":"2b7df46e8ae1517d64c896c7793d2d52ec845669","analyzedAt":"2026-09-01T07:59:23.713Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}