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
- 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.
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
- Test patterns with new RegExp() before passing them in.
- Avoid lookbehind on runtimes below ES2018, or upgrade Node.
- Balance all brackets, parens, and braces.
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
- Invalid regex. Details: ${err.message}
- Invalid input. Enter either a CIDR range (e.g. 10.0.0.0/24)
- Invalid IPv6 address
- Error: Invalid output format
- Invalid regex. Details: ${err.message}
AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13).
Data as JSON: /api/errors/7da37cf0eb38acdc.
Report an issue: GitHub.