{"record":{"id":"6178f2119922ce8b","repo":"yamadashy/repomix","slug":"invalid-regular-expression-pattern-pattern","errorCode":null,"errorMessage":"Invalid regular expression pattern: ${pattern}. ${error instanceof Error ? error.message : String(error)}","messagePattern":"Invalid regular expression pattern: (.+?)\\. (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/mcp/tools/grepRepomixOutputTool.ts","lineNumber":228,"sourceCode":"    },\n  );\n};\n\n/**\n * Create and validate a regular expression pattern\n */\nexport const createRegexPattern = (\n  pattern: string,\n  ignoreCase: boolean,\n  deps = {\n    RegExp,\n  },\n): RegExp => {\n  const regexFlags = ignoreCase ? 'gi' : 'g';\n  try {\n    return new deps.RegExp(pattern, regexFlags);\n  } catch (error) {\n    throw new Error(\n      `Invalid regular expression pattern: ${pattern}. ${error instanceof Error ? error.message : String(error)}`,\n    );\n  }\n};\n\n/**\n * Search for pattern matches in file content\n */\nexport const searchInContent = (\n  content: string,\n  options: SearchOptions,\n  deps = {\n    createRegexPattern,\n  },\n): SearchMatch[] => {\n  return searchInLines(content.split('\\n'), options, deps);\n};\n","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/yamadashy/repomix/blob/f465ad909315a22120636baf03fa5e28701a50cb/src/mcp/tools/grepRepomixOutputTool.ts#L210-L246","documentation":"createRegexPattern wraps the RegExp constructor so that an invalid user-supplied pattern becomes a clear error naming the pattern and the underlying regex engine message. Thrown by the grep repomix output MCP tool when the `pattern` argument cannot be compiled.","triggerScenarios":"Calling the grep tool with a syntactically invalid regex, e.g. unbalanced parentheses `'(foo'`, a bad quantifier `'*abc'`, an unterminated character class `'[a-'`, or an invalid escape `'\\\\p{Foo}/u'` semantics.","commonSituations":"Users paste shell/PCRE-flavored patterns not valid in JavaScript (e.g. lookbehind on old engines, `\\\\A`/`\\\\z` anchors); unescaped special characters from interpolated user input; quotes stripping backslashes so `\\\\d` arrives as `d`.","solutions":["Fix the pattern syntax per JavaScript RegExp rules (test it in a JS console first)","Escape special characters that should be literal, e.g. `foo\\\\.bar` for a dot","If interpolating user input, use a regex-escape helper before building the pattern","Simplify the pattern incrementally to isolate the offending token"],"exampleFix":"// before\nawait grepRepomixOutput({ pattern: '(import|export' });\n// after\nawait grepRepomixOutput({ pattern: '(import|export)' });","handlingStrategy":"validation","validationCode":"function isSafeRegex(pattern) {\n  try { new RegExp(pattern, 'g'); return true; } catch { return false; }\n}\nif (!isSafeRegex(pattern)) throw new Error(`Invalid regex: ${pattern}`);","typeGuard":"const isValidRegex = (p) => { try { new RegExp(p, 'g'); return true; } catch { return false; } };","tryCatchPattern":"try {\n  await grepRepomixOutput({ pattern });\n} catch (e) {\n  if (String(e.message).startsWith('Invalid regular expression pattern')) {\n    // escape/simplify the pattern and retry\n  } else throw e;\n}","preventionTips":["Test patterns in a JS console before use","Escape metacharacters from user input with an escapeRegExp helper","Avoid PCRE-only syntax (\\A, \\z) in JS regexes"],"tags":["mcp","regex","validation"],"backgroundTag":"invalid-regex-pattern","analyzedSha":"f465ad909315a22120636baf03fa5e28701a50cb","analyzedAt":"2026-08-29T01:27:42.024Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}