CherryHQ/cherry-studio · warning · Error

Invalid regex pattern: ${data.pattern}

Error message

Invalid regex pattern: ${data.pattern}

What it means

new RegExp(pattern, 'i') threw a SyntaxError because the pattern string is not a valid JavaScript regex — unclosed groups, unbalanced brackets, invalid escape sequences, or stray quantifiers. The handler catches it and rethrows with the offending pattern so the caller can correct it. Note ripgrep received the raw string too (via '--', pushed at grep.ts:85) and would also fail; the JS regex construction is what surfaces the error first in the manual fallback path.

Source

Thrown at src/main/ai/mcp/servers/filesystem/tools/grep.ts:93

    for (const pat of data.include
      .split(',')
      .map((p) => p.trim())
      .filter(Boolean)) {
      rgArgs.push('--glob', pat)
    }
  }

  // `--` ends ripgrep option parsing so a pattern like `--pre=<cmd>` is treated as a
  // literal search pattern instead of the preprocessor flag (arg-injection → RCE).
  rgArgs.push('--', data.pattern, validPath)

  try {
    // No `g` flag: this regex is reused with `.test(line)` per line, and a global
    // regex carries `lastIndex` across calls — that silently skips matches on
    // subsequent lines. Case-insensitive matching only.
    regex = new RegExp(data.pattern, 'i')
  } catch (error) {
    throw new Error(`Invalid regex pattern: ${data.pattern}`)
  }

  async function searchFile(filePath: string): Promise<void> {
    if (matches.length >= MAX_GREP_MATCHES) {
      truncated = true
      return
    }

    try {
      // Skip binary files
      if (await isBinaryFile(filePath)) {
        return
      }

      const content = await fs.readFile(filePath, 'utf-8')
      const lines = content.split('\n')

      lines.forEach((line, index) => {

View on GitHub (pinned to 726446b54c)

Solutions

  1. Balance all groups and character classes in the pattern; escape literal ( ) [ ] { } * + ? . \ ^ $ | with backslashes.
  2. Test the pattern with `new RegExp(pattern)` in a scratch console before sending.
  3. If the pattern works in ripgrep but not JS, simplify it to syntax common to both engines (the tool uses JS regex for the fallback search and ripgrep for the primary path).
  4. Validate the pattern with try/catch around RegExp construction on the client side before dispatching.

Example fix

// before
try {
  regex = new RegExp(data.pattern, 'i')
} catch (error) {
  throw new Error(`Invalid regex pattern: ${data.pattern}`)
}

// after — include the underlying SyntaxError reason
try {
  regex = new RegExp(data.pattern, 'i')
} catch (error) {
  const reason = error instanceof Error ? error.message : 'syntax error'
  throw new Error(`Invalid regex pattern: ${data.pattern} (${reason})`)
}
Defensive patterns

Strategy: validation

Validate before calling

// Compile the regex before dispatching so syntax errors surface client-side.
function isValidRegex(p: string): boolean {
  try { new RegExp(p, 'i'); return true } catch { return false }
}
if (!isValidRegex(pattern)) throw new Error(`Client rejected pattern: ${pattern}`)

Try / catch

// Compile defensively and report the syntax reason.
let regex: RegExp
try {
  regex = new RegExp(data.pattern, 'i')
} catch (e) {
  const reason = e instanceof Error ? e.message : 'syntax error'
  throw new Error(`Invalid regex pattern: ${data.pattern} (${reason})`)
}

Prevention

When it happens

Trigger: Pattern contains unbalanced parentheses (e.g. 'function\('), dangling quantifiers (e.g. '*' at the start), unclosed character classes (e.g. '[a-z'), or invalid escapes in non-strict contexts. The error fires in the try at grep.ts:91 before the search loops run.

Common situations: A model emitting a regex with an unescaped parenthesis meant literally; a user-supplied pattern copied from a POSIX-only engine (different escape rules); a pattern that is valid in ripgrep's Rust regex crate but not in JavaScript's (e.g. some lookbehind or atomic-group syntaxes).

Related errors


AI-assisted analysis of CherryHQ/cherry-studio@726446b54c (2026-08-12). Data as JSON: /api/errors/5c1fcf82a655eabb. Report an issue: GitHub.