{"record":{"id":"c7007ba80f90f8de","repo":"gchq/CyberChef","slug":"invalid-regex-details-err-message","errorCode":null,"errorMessage":"Invalid regex. Details: ${err.message}","messagePattern":"Invalid regex\\. Details: (.+?)","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/Filter.mjs","lineNumber":61,"sourceCode":"                \"value\": false\n            }\n        ];\n    }\n\n    /**\n     * @param {string} input\n     * @param {Object[]} args\n     * @returns {string}\n     */\n    run(input, args) {\n        const delim = Utils.charRep(args[0]),\n            reverse = args[2];\n        let regex;\n\n        try {\n            regex = new XRegExp(args[1]);\n        } catch (err) {\n            throw new OperationError(`Invalid regex. Details: ${err.message}`);\n        }\n\n        const regexFilter = function(value) {\n            return reverse ^ regex.test(value);\n        };\n\n        return input.split(delim).filter(regexFilter).join(delim);\n    }\n\n}\n\nexport default Filter;\n","sourceCodeStart":43,"sourceCodeEnd":74,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/Filter.mjs#L43-L74","documentation":"Thrown by the Filter operation when the XRegExp constructor fails to parse the user-supplied regex string. The operation splits input by a delimiter and filters lines matching the regex. An invalid regex pattern (unbalanced parentheses, bad escape sequences, invalid quantifiers) prevents compilation.","triggerScenarios":"run(input, args) where new XRegExp(args[1]) throws a SyntaxError. The regex string is the second argument ('Regex'), and XRegExp re-throws native RegExp syntax errors.","commonSituations":"Typo in regex: unclosed groups '(', unescaped special chars, invalid quantifier '{3,2}', or unsupported XRegExp syntax. Also when copy-pasting a regex with incompatible flags or PCRE-specific syntax not supported by JS regex.","solutions":["Test the regex in a regex tester (regex101.com with JavaScript flavor) before using it.","Balance all parentheses, brackets, and braces.","Escape special characters (., *, +, ?, ^, $, {, }, [, ], \\, |, (, )) that should match literally.","Check the wrapped err.message for the specific syntax error position."],"exampleFix":"// before: args[1] = 'foo(bar' (unclosed group) -> SyntaxError\n\n// after: args[1] = 'foo(bar)'","handlingStrategy":"validation","validationCode":"// Validate regex compiles before calling Filter\ntry {\n  new RegExp(args[1]);\n} catch (e) {\n  throw new Error('Invalid regex: ' + e.message);\n}","typeGuard":"function isValidRegex(pattern) {\n  try { new RegExp(pattern); return true; }\n  catch { return false; }\n}","tryCatchPattern":null,"preventionTips":["Test regex patterns in a JavaScript-compatible tester before use.","Balance all grouping constructs.","Escape special characters that should match literally."],"tags":["regex","validation","syntax-error","xregexp"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}