gchq/CyberChef · error · OperationError

Invalid Regular Expression (Please note this version of node

Error message

Invalid Regular Expression (Please note this version of node does not support look behinds).

What it means

To Case Insensitive Regex first validates the input by constructing `RegExp(input)`. If the input is not a syntactically valid regular expression, this throws and is rewrapped as an OperationError. The message also notes that the Node version may not support lookbehind assertions ((?<=...) / (?<!...)), which is a common cause of RegExp construction failures on older runtimes.

Source

Thrown at src/core/operations/ToCaseInsensitiveRegex.mjs:59

         * @param {string} input
         * @returns {string}
         */
        function preProcess(input) {
            let result = "";
            for (let i = 0; i < input.length; i++) {
                const temp = input.charAt(i);
                if (temp.match(/[a-zA-Z]/g) && (input.charAt(i-1) !== "-") && (input.charAt(i+1) !== "-"))
                    result += "[" + temp.toLowerCase() + temp.toUpperCase() + "]";
                else
                    result += temp;
            }
            return result;
        }

        try {
            RegExp(input);
        } catch (error) {
            throw new OperationError("Invalid Regular Expression (Please note this version of node does not support look behinds).");
        }

        // Example: [test] -> [[tT][eE][sS][tT]]
        return preProcess(input)

            // Example: [A-Z] -> [A-Za-z]
            .replace(/([A-Z]-[A-Z]|[a-z]-[a-z])/g, m => `${m[0].toUpperCase()}-${m[2].toUpperCase()}${m[0].toLowerCase()}-${m[2].toLowerCase()}`)

            // Example: [H-d] -> [A-DH-dh-z]
            .replace(/[A-Z]-[a-z]/g, m => `A-${m[2].toUpperCase()}${m}${m[0].toLowerCase()}-z`)

            // Example: [!-D] -> [!-Da-d]
            .replace(/\\?[ -@]-[A-Z]/g, m => `${m}a-${m[2].toLowerCase()}`)

            // Example: [%-^] -> [%-^a-z]
            .replace(/\\?[ -@]-\\?[[-`]/g, m => `${m}a-z`)

            // Example: [K-`] -> [K-`k-z]

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Fix the regex syntax: balance brackets, parens, and braces.
  2. Remove lookbehind ((?<=...) / (?<!...)) if running on a pre-ES2018 runtime, or upgrade Node.
  3. Validate the pattern with `new RegExp(pattern)` in a REPL before using it.

Example fix

// before: input = "(?<=foo)bar" on Node <10  -> throws (no lookbehind support)
// after:  input = "(?:foo)?bar"               -> valid, converts to case-insensitive form
Defensive patterns

Strategy: try-catch

Validate before calling

try { new RegExp(pattern); }
catch (e) { throw new Error(`Pattern is not a valid RegExp: ${e.message}`); }

Type guard

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

Try / catch

try { chef.ToCaseInsensitiveRegex(pattern, []); }
catch (e) { if (/Invalid Regular Expression/.test(e.message)) { /* fix syntax or drop lookbehind */ } else throw e; }

Prevention

When it happens

Trigger: Feeding a malformed regex string such as an unbalanced bracket `[a-z`, an unmatched parenthesis `(abc`, a dangling quantifier `*abc`, or a lookbehind on a runtime older than ES2018.

Common situations: Pasting a regex copied from a PCRE/Python source that uses lookbehind, into an older Node build; truncating a regex when pasting; using PCRE-only syntax unsupported by JavaScript.

Related errors


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