{"record":{"id":"a7f4dd77de5a4fdf","repo":"gchq/CyberChef","slug":"invalid-regex-details-err-message-a7f4dd","errorCode":null,"errorMessage":"Invalid regex. Details: ${err.message}","messagePattern":"Invalid regex\\. Details: (.+?)","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/RegularExpression.mjs","lineNumber":174,"sourceCode":"\n        if (userRegex && userRegex !== \"^\" && userRegex !== \"$\") {\n            try {\n                const regex = new XRegExp(userRegex, modifiers);\n\n                switch (outputFormat) {\n                    case \"Highlight matches\":\n                        return regexHighlight(input, regex, displayTotal);\n                    case \"List matches\":\n                        return Utils.escapeHtml(regexList(input, regex, displayTotal, true, false));\n                    case \"List capture groups\":\n                        return Utils.escapeHtml(regexList(input, regex, displayTotal, false, true));\n                    case \"List matches with capture groups\":\n                        return Utils.escapeHtml(regexList(input, regex, displayTotal, true, true));\n                    default:\n                        throw new OperationError(\"Error: Invalid output format\");\n                }\n            } catch (err) {\n                throw new OperationError(\"Invalid regex. Details: \" + err.message);\n            }\n        } else {\n            return Utils.escapeHtml(input);\n        }\n    }\n\n}\n\n/**\n * Creates a string listing the matches within a string.\n *\n * @param {string} input\n * @param {RegExp} regex\n * @param {boolean} displayTotal\n * @param {boolean} matches - Display full match\n * @param {boolean} captureGroups - Display each of the capture groups separately\n * @returns {string}\n */","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/RegularExpression.mjs#L156-L192","documentation":"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.","triggerScenarios":"Patterns with unbalanced groups 'a(b', invalid backreferences, invalid quantifier ranges 'a{3,1}', unsupported/typo'd flags, or XRegExp-incompatible constructs.","commonSituations":"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.","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."],"exampleFix":"// before\n//   pattern: \"a(b\"  -> details: Invalid regular expression\n// after\n//   pattern: \"a(b)c\"","handlingStrategy":"try-catch","validationCode":"try { new XRegExp(userRegex, modifiers); } catch (e) { throw new Error('Pattern will fail: ' + e.message); }","typeGuard":"function isValidRegex(pat, mods) { try { new XRegExp(pat, mods); return true; } catch { return false; } }","tryCatchPattern":"try { runRegex(input, { userRegex }); } catch (e) { if (/Invalid regex/.test(e.message)) fixPattern(e.message); else throw e; }","preventionTips":["Validate the pattern by constructing XRegExp before running.","Test patterns in regex101 JS mode.","Avoid flags unsupported by XRegExp."],"tags":["regex","xregexp","validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}