{"record":{"id":"7da37cf0eb38acdc","repo":"gchq/CyberChef","slug":"invalid-regular-expression-please-note-this-versi","errorCode":null,"errorMessage":"Invalid Regular Expression (Please note this version of node does not support look behinds).","messagePattern":"Invalid Regular Expression \\(Please note this version of node does not support look behinds\\)\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/ToCaseInsensitiveRegex.mjs","lineNumber":59,"sourceCode":"         * @param {string} input\n         * @returns {string}\n         */\n        function preProcess(input) {\n            let result = \"\";\n            for (let i = 0; i < input.length; i++) {\n                const temp = input.charAt(i);\n                if (temp.match(/[a-zA-Z]/g) && (input.charAt(i-1) !== \"-\") && (input.charAt(i+1) !== \"-\"))\n                    result += \"[\" + temp.toLowerCase() + temp.toUpperCase() + \"]\";\n                else\n                    result += temp;\n            }\n            return result;\n        }\n\n        try {\n            RegExp(input);\n        } catch (error) {\n            throw new OperationError(\"Invalid Regular Expression (Please note this version of node does not support look behinds).\");\n        }\n\n        // Example: [test] -> [[tT][eE][sS][tT]]\n        return preProcess(input)\n\n            // Example: [A-Z] -> [A-Za-z]\n            .replace(/([A-Z]-[A-Z]|[a-z]-[a-z])/g, m => `${m[0].toUpperCase()}-${m[2].toUpperCase()}${m[0].toLowerCase()}-${m[2].toLowerCase()}`)\n\n            // Example: [H-d] -> [A-DH-dh-z]\n            .replace(/[A-Z]-[a-z]/g, m => `A-${m[2].toUpperCase()}${m}${m[0].toLowerCase()}-z`)\n\n            // Example: [!-D] -> [!-Da-d]\n            .replace(/\\\\?[ -@]-[A-Z]/g, m => `${m}a-${m[2].toLowerCase()}`)\n\n            // Example: [%-^] -> [%-^a-z]\n            .replace(/\\\\?[ -@]-\\\\?[[-`]/g, m => `${m}a-z`)\n\n            // Example: [K-`] -> [K-`k-z]","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/ToCaseInsensitiveRegex.mjs#L41-L77","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Fix the regex syntax: balance brackets, parens, and braces.","Remove lookbehind ((?<=...) / (?<!...)) if running on a pre-ES2018 runtime, or upgrade Node.","Validate the pattern with `new RegExp(pattern)` in a REPL before using it."],"exampleFix":"// before: input = \"(?<=foo)bar\" on Node <10  -> throws (no lookbehind support)\n// after:  input = \"(?:foo)?bar\"               -> valid, converts to case-insensitive form","handlingStrategy":"try-catch","validationCode":"try { new RegExp(pattern); }\ncatch (e) { throw new Error(`Pattern is not a valid RegExp: ${e.message}`); }","typeGuard":"function isValidRegex(pattern) {\n  try { new RegExp(pattern); return true; } catch { return false; }\n}","tryCatchPattern":"try { chef.ToCaseInsensitiveRegex(pattern, []); }\ncatch (e) { if (/Invalid Regular Expression/.test(e.message)) { /* fix syntax or drop lookbehind */ } else throw e; }","preventionTips":["Test patterns with new RegExp() before passing them in.","Avoid lookbehind on runtimes below ES2018, or upgrade Node.","Balance all brackets, parens, and braces."],"tags":["regex","syntax","lookbehind","validation","runtime"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}