{"record":{"id":"b5edc89321ec89b2","repo":"can1357/oh-my-pi","slug":"invalid-regular-expression-src-slice-i-end","errorCode":null,"errorMessage":"invalid regular expression \"${src.slice(i, end)}\"","messagePattern":"invalid regular expression \"(.+?)\"","errorType":"exception","errorClass":"OmpTypeError","httpStatus":null,"severity":"error","filePath":"packages/omptype/src/ir.ts","lineNumber":234,"sourceCode":"\t\t\ttoks.push({ t: \"str\", v: value });\n\t\t\ti = j + 1;\n\t\t\tcontinue;\n\t\t}\n\t\tif (c === \"/\") {\n\t\t\tlet j = i + 1;\n\t\t\tfor (; j < n; j++) {\n\t\t\t\tif (src[j] === \"\\\\\") j++;\n\t\t\t\telse if (src[j] === \"/\") break;\n\t\t\t}\n\t\t\tif (j >= n) throw new OmpTypeError(`unterminated regular expression in \"${src}\"`);\n\t\t\tlet end = j + 1;\n\t\t\twhile (end < n && /[dgimsuvy]/.test(src[end])) end++;\n\t\t\tconst source = src.slice(i + 1, j);\n\t\t\tconst flags = src.slice(j + 1, end);\n\t\t\ttry {\n\t\t\t\ttoks.push({ t: \"regex\", v: new RegExp(source, flags) });\n\t\t\t} catch {\n\t\t\t\tthrow new OmpTypeError(`invalid regular expression \"${src.slice(i, end)}\"`);\n\t\t\t}\n\t\t\ti = end;\n\t\t\tcontinue;\n\t\t}\n\t\tif ((c >= \"0\" && c <= \"9\") || (c === \"-\" && i + 1 < n && src[i + 1] >= \"0\" && src[i + 1] <= \"9\")) {\n\t\t\tlet j = i + 1;\n\t\t\twhile (j < n && /[\\w.+-]/.test(src[j])) j++;\n\t\t\tconst raw = src.slice(i, j);\n\t\t\tif (/^-?(?:0|[1-9]\\d*)n$/.test(raw) && raw !== \"-0n\") {\n\t\t\t\ttoks.push({ t: \"bigint\", v: BigInt(raw.slice(0, -1)) });\n\t\t\t} else {\n\t\t\t\tconst valid =\n\t\t\t\t\t/^-?(?:0|[1-9]\\d*)(?:\\.\\d+)?$/.test(raw) && !Object.is(Number(raw), -0) && String(Number(raw)) === raw;\n\t\t\t\tif (!valid) throw new OmpTypeError(`Malformed number literal '${raw}'`);\n\t\t\t\ttoks.push({ t: \"num\", v: Number(raw) });\n\t\t\t}\n\t\t\ti = j;\n\t\t\tcontinue;","sourceCodeStart":216,"sourceCodeEnd":252,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/omptype/src/ir.ts#L216-L252","documentation":"A properly delimited regex literal `/.../flags` was tokenized, but `new RegExp(source, flags)` threw — the pattern or its flags are syntactically invalid JavaScript. The tokenizer rethrows as `OmpTypeError` including the literal text.","triggerScenarios":"Malformed patterns like `/(/` (unclosed group), `/[a-/`, `/(?<name/x)/` (invalid group), or invalid flag combos like `/x/yy` (duplicate flag).","commonSituations":"Regexes authored for another flavor (PCRE/RE2 constructs like lookbehind variants unsupported by the JS engine version); duplicated or typo'd flags; hand-edited patterns with unbalanced brackets.","solutions":["Test the pattern with `new RegExp(p, flags)` in a REPL to see the underlying SyntaxError.","Remove invalid/duplicate flags; valid flags are d,g,i,m,s,u,v,y.","Replace engine-unsupported constructs (e.g. `(?R)`, `\\K`) with JS-compatible equivalents.","Pre-validate the regex with a try/catch around `new RegExp` before embedding it in the expression."],"exampleFix":"// before\ntype(`value ~= /[a-z+/`); // unbalanced class\n// after\ntype(`value ~= /[a-z]+/`);","handlingStrategy":"validation","validationCode":"function assertValidRegex(pattern: string, flags: string): void {\n  try { new RegExp(pattern, flags); } catch (e) {\n    throw new Error(`Invalid regex /${pattern}/${flags}: ${(e as Error).message}`);\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  const t = new TypeExpression(src);\n} catch (err) {\n  if (err instanceof Error && err.message.startsWith(\"invalid regular expression\")) {\n    throw new Error(`Fix pattern syntax: ${err.message}`);\n  }\n  throw err;\n}","preventionTips":["Test every pattern with `new RegExp` before embedding it in an expression.","Use only flags d,g,i,m,s,u,v,y without duplicates.","Avoid non-JS regex flavors (PCRE/RE2-only constructs); keep the `u` flag in mind for escape rules."],"tags":["parser","regex","invalid-regex"],"backgroundTag":"invalid-regex-pattern","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}