gchq/CyberChef · error · OperationError

Invalid regex. Details: ${err.message}

Error message

Invalid regex. Details: ${err.message}

What it means

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.

Source

Thrown at src/core/operations/Filter.mjs:61

                "value": false
            }
        ];
    }

    /**
     * @param {string} input
     * @param {Object[]} args
     * @returns {string}
     */
    run(input, args) {
        const delim = Utils.charRep(args[0]),
            reverse = args[2];
        let regex;

        try {
            regex = new XRegExp(args[1]);
        } catch (err) {
            throw new OperationError(`Invalid regex. Details: ${err.message}`);
        }

        const regexFilter = function(value) {
            return reverse ^ regex.test(value);
        };

        return input.split(delim).filter(regexFilter).join(delim);
    }

}

export default Filter;

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Test the regex in a regex tester (regex101.com with JavaScript flavor) before using it.
  2. Balance all parentheses, brackets, and braces.
  3. Escape special characters (., *, +, ?, ^, $, {, }, [, ], \, |, (, )) that should match literally.
  4. Check the wrapped err.message for the specific syntax error position.

Example fix

// before: args[1] = 'foo(bar' (unclosed group) -> SyntaxError

// after: args[1] = 'foo(bar)'
Defensive patterns

Strategy: validation

Validate before calling

// Validate regex compiles before calling Filter
try {
  new RegExp(args[1]);
} catch (e) {
  throw new Error('Invalid regex: ' + e.message);
}

Type guard

function isValidRegex(pattern) {
  try { new RegExp(pattern); return true; }
  catch { return false; }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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