gchq/CyberChef · error · OperationError

Invalid regex. Details: ${err.message}

Error message

Invalid regex. Details: ${err.message}

What it means

Thrown by the RegularExpression operation when constructing or running the XRegExp pattern fails. The inner catch wraps the underlying engine error and prepends 'Invalid regex. Details:'. Causes include syntax errors, unbalanced parentheses, invalid quantifiers, or unsupported flags.

Source

Thrown at src/core/operations/RegularExpression.mjs:174

        if (userRegex && userRegex !== "^" && userRegex !== "$") {
            try {
                const regex = new XRegExp(userRegex, modifiers);

                switch (outputFormat) {
                    case "Highlight matches":
                        return regexHighlight(input, regex, displayTotal);
                    case "List matches":
                        return Utils.escapeHtml(regexList(input, regex, displayTotal, true, false));
                    case "List capture groups":
                        return Utils.escapeHtml(regexList(input, regex, displayTotal, false, true));
                    case "List matches with capture groups":
                        return Utils.escapeHtml(regexList(input, regex, displayTotal, true, true));
                    default:
                        throw new OperationError("Error: Invalid output format");
                }
            } catch (err) {
                throw new OperationError("Invalid regex. Details: " + err.message);
            }
        } else {
            return Utils.escapeHtml(input);
        }
    }

}

/**
 * Creates a string listing the matches within a string.
 *
 * @param {string} input
 * @param {RegExp} regex
 * @param {boolean} displayTotal
 * @param {boolean} matches - Display full match
 * @param {boolean} captureGroups - Display each of the capture groups separately
 * @returns {string}
 */

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Test the pattern in a regex playground (regex101 in JS/PCRE mode) and fix the syntax error from err.message.
  2. Balance all groups and brackets; escape literal special chars.
  3. Verify the flags string contains only valid XRegExp flags (g, i, m, s, x, etc.).
  4. Simplify the pattern incrementally to isolate the failing segment.

Example fix

// before
//   pattern: "a(b"  -> details: Invalid regular expression
// after
//   pattern: "a(b)c"
Defensive patterns

Strategy: try-catch

Validate before calling

try { new XRegExp(userRegex, modifiers); } catch (e) { throw new Error('Pattern will fail: ' + e.message); }

Type guard

function isValidRegex(pat, mods) { try { new XRegExp(pat, mods); return true; } catch { return false; } }

Try / catch

try { runRegex(input, { userRegex }); } catch (e) { if (/Invalid regex/.test(e.message)) fixPattern(e.message); else throw e; }

Prevention

When it happens

Trigger: Patterns with unbalanced groups 'a(b', invalid backreferences, invalid quantifier ranges 'a{3,1}', unsupported/typo'd flags, or XRegExp-incompatible constructs.

Common situations: User-typed pattern with a typo; pattern written for a different regex flavor (PCRE vs JS vs XRegExp); unescaped special characters; copy-paste introducing smart quotes.

Related errors


AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13). Data as JSON: /api/errors/a7f4dd77de5a4fdf. Report an issue: GitHub.