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
- Test the pattern in a regex playground (regex101 in JS/PCRE mode) and fix the syntax error from err.message.
- Balance all groups and brackets; escape literal special chars.
- Verify the flags string contains only valid XRegExp flags (g, i, m, s, x, etc.).
- 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
- Validate the pattern by constructing XRegExp before running.
- Test patterns in regex101 JS mode.
- Avoid flags unsupported by XRegExp.
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
- Invalid regex. Details: ${err.message}
- Invalid input. Enter either a CIDR range (e.g. 10.0.0.0/24)
- Invalid IPv6 address
- Error: Invalid output format
- Invalid Regular Expression (Please note this version of node
AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13).
Data as JSON: /api/errors/a7f4dd77de5a4fdf.
Report an issue: GitHub.