{"record":{"id":"0878a671ea4a2ba8","repo":"different-ai/openwork","slug":"string-method-expects-a-regular-expression-a","errorCode":null,"errorMessage":"String.${method} expects a regular expression (a /pattern/flags literal or new RegExp(...)) or a string pattern, not ${arg === null ? \"null\" : typeof arg}.","messagePattern":"String\\.(.+?) expects a regular expression \\(a /pattern/flags literal or new RegExp\\(\\.\\.\\.\\)\\) or a string pattern, not (.+?)\\.","errorType":"exception","errorClass":"InterpreterRuntimeError","httpStatus":null,"severity":"error","filePath":"packages/codemode/src/stdlib/regexp.ts","lineNumber":33,"sourceCode":"export const regexFailureReason = (error: unknown): string =>\n  (error instanceof Error ? error.message : String(error)).replace(/^Invalid regular expression:\\s*/i, \"\")\n\nexport const escapeRegexHint =\n  'To match special characters like ( ) [ ] { } + * ? . literally, escape them with a backslash (e.g. \"\\\\\\\\(\") or test for them with String.includes instead.'\n\nexport const toHostRegex = (arg: unknown, method: string, node: AstNode, extraFlags = \"\"): RegExp => {\n  if (arg instanceof SandboxRegExp) return arg.regex\n  if (typeof arg === \"string\") {\n    try {\n      return new RegExp(arg, extraFlags)\n    } catch (error) {\n      throw new InterpreterRuntimeError(\n        `String.${method} received the string ${JSON.stringify(arg)}, which is not a valid regular expression pattern (${regexFailureReason(error)}). ${escapeRegexHint}`,\n        node,\n      ).as(\"SyntaxError\")\n    }\n  }\n  throw new InterpreterRuntimeError(\n    `String.${method} expects a regular expression (a /pattern/flags literal or new RegExp(...)) or a string pattern, not ${arg === null ? \"null\" : typeof arg}.`,\n    node,\n  )\n}\n\nexport const matchToValue = (match: RegExpMatchArray): Array<unknown> => {\n  const result: Array<unknown> = Array.from(match, (group) => group)\n  if (match.index !== undefined) (result as Record<string, unknown> & Array<unknown>).index = match.index\n  if (match.groups) {\n    const groups: SafeObject = Object.create(null) as SafeObject\n    for (const [key, group] of Object.entries(match.groups)) {\n      if (!isBlockedMember(key)) groups[key] = group\n    }\n    ;(result as Record<string, unknown> & Array<unknown>).groups = groups\n  }\n  return result\n}\n","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/different-ai/openwork/blob/2b7df46e8ae1517d64c896c7793d2d52ec845669/packages/codemode/src/stdlib/regexp.ts#L15-L51","documentation":"CodeMode's sandboxed String regex methods (match, replace, split, search) accept only a /pattern/flags RegExp literal, a `new RegExp(...)` value, or a plain string pattern. This throw happens when the pattern argument is some other type (null, number, object, array, undefined). The library throws early so the host runtime never sees non-pattern data passed to its safe RegExp wrapper.","triggerScenarios":"Calling e.g. `\"abc\".match(123)`, `\"abc\".replace(null, \"x\")`, `\"a,b\".split([\",\"])`, or passing an undefined variable as the pattern argument to any String regex method.","commonSituations":"Variables intended to hold a pattern end up null because an upstream lookup failed; JSON-decoded data yields numbers/objects where a pattern string was expected; developers forget to wrap a pattern in RegExp or quotes.","solutions":["Coerce the pattern to a string first: `\"abc\".match(String(pattern))`.","Wrap dynamic patterns in `new RegExp(String(raw), flags)` before use.","Fix the upstream value so it is a string or RegExp, not null/undefined/number.","If the value may legitimately be absent, default it: `pattern ?? \"\"`."],"exampleFix":"// before\nconst parts = csv.split(delimiter) // delimiter is null\n// after\nconst parts = csv.split(new RegExp(String(delimiter ?? \",\")))","handlingStrategy":"type-guard","validationCode":"function isPattern(p) { return typeof p === \"string\" || p instanceof RegExp }\nif (!isPattern(pattern)) throw new TypeError(\"pattern must be a string or RegExp\")","typeGuard":"const isRegExpLike = (v) => typeof v === \"string\" || v instanceof RegExp","tryCatchPattern":"try { result = str.match(pattern) } catch (e) { if (String(e).includes(\"expects a regular expression\")) result = null }","preventionTips":["Default pattern variables to \"\" at declaration","Coerce dynamic patterns with new RegExp(String(raw))","Validate JSON-derived inputs before regex use"],"tags":["codemode","regex","type-error","sandbox"],"backgroundTag":"invalid-regexp-pattern","analyzedSha":"2b7df46e8ae1517d64c896c7793d2d52ec845669","analyzedAt":"2026-09-01T07:59:23.713Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}