yamadashy/repomix · error

Invalid regular expression pattern: ${pattern}. ${error inst

Error message

Invalid regular expression pattern: ${pattern}. ${error instanceof Error ? error.message : String(error)}

What it means

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.

Source

Thrown at src/mcp/tools/grepRepomixOutputTool.ts:228

    },
  );
};

/**
 * Create and validate a regular expression pattern
 */
export const createRegexPattern = (
  pattern: string,
  ignoreCase: boolean,
  deps = {
    RegExp,
  },
): RegExp => {
  const regexFlags = ignoreCase ? 'gi' : 'g';
  try {
    return new deps.RegExp(pattern, regexFlags);
  } catch (error) {
    throw new Error(
      `Invalid regular expression pattern: ${pattern}. ${error instanceof Error ? error.message : String(error)}`,
    );
  }
};

/**
 * Search for pattern matches in file content
 */
export const searchInContent = (
  content: string,
  options: SearchOptions,
  deps = {
    createRegexPattern,
  },
): SearchMatch[] => {
  return searchInLines(content.split('\n'), options, deps);
};

View on GitHub (pinned to f465ad9093)

Solutions

  1. Fix the pattern syntax per JavaScript RegExp rules (test it in a JS console first)
  2. Escape special characters that should be literal, e.g. `foo\\.bar` for a dot
  3. If interpolating user input, use a regex-escape helper before building the pattern
  4. Simplify the pattern incrementally to isolate the offending token

Example fix

// before
await grepRepomixOutput({ pattern: '(import|export' });
// after
await grepRepomixOutput({ pattern: '(import|export)' });
Defensive patterns

Strategy: validation

Validate before calling

function isSafeRegex(pattern) {
  try { new RegExp(pattern, 'g'); return true; } catch { return false; }
}
if (!isSafeRegex(pattern)) throw new Error(`Invalid regex: ${pattern}`);

Type guard

const isValidRegex = (p) => { try { new RegExp(p, 'g'); return true; } catch { return false; } };

Try / catch

try {
  await grepRepomixOutput({ pattern });
} catch (e) {
  if (String(e.message).startsWith('Invalid regular expression pattern')) {
    // escape/simplify the pattern and retry
  } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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`.

Related errors


AI-assisted analysis of yamadashy/repomix@f465ad9093 (2026-08-29). Data as JSON: /api/errors/6178f2119922ce8b. Report an issue: GitHub.