karatelabs/karate · error · SyntaxError
Invalid regular expression: /
Error message
Invalid regular expression: /
What it means
After translating the JS regex source to Java syntax, the pattern is compiled with java.util.regex.Pattern. If Pattern.compile raises PatternSyntaxException, a SyntaxError 'Invalid regular expression: /pattern/ - <detail>' is thrown. This means the pattern is syntactically invalid even after Karate's JS→Java translation (or the translation itself does not cover the construct used).
Solutions
- Read the PatternSyntaxException detail appended to the message for the exact position/problem
- Fix the pattern: balance parens/brackets, correct quantifiers and escapes
- Validate the pattern with a quick try/catch around new RegExp(pattern) before using it
- Drop or rewrite JS-only constructs (e.g. use ([A-Za-z]) instead of \p{Alpha} when flags/translation do not support it)
Example fix
// before
new RegExp('a('); // unbalanced group -> PatternSyntaxException
// after
new RegExp('a(bc)?'); // balanced, valid pattern Defensive patterns
Strategy: validation
Validate before calling
function isValidPattern(p) {
try { new RegExp(p); return true; } catch (e) { return false; }
}
if (!isValidPattern(pattern)) throw new Error('invalid regex: ' + pattern); Try / catch
try {
const re = new RegExp(pattern, flags);
} catch (e) {
if (String(e.message).startsWith('Invalid regular expression')) {
log.warn('bad regex pattern: ' + pattern + ' :: ' + e.message);
return fallbackRegex;
}
throw e;
} Prevention
- Validate dynamically built patterns at the point of construction
- Balance parentheses/brackets and check quantifier placement in generated patterns
- Prefer widely-supported constructs over JS-only syntax the translator may not cover
- Test config/env-sourced patterns with a smoke compile at startup
When it happens
Trigger: Patterns with unbalanced parentheses or brackets, invalid quantifiers like /*/, invalid escapes like \q, unterminated groups, or JS-only syntax (e.g. lookbehind forms, unicode property escapes \p{...} without the u flag) that translate to something Java rejects.
Common situations: Dynamically building patterns from user input; copying a JS regex using lookbehind or named-group syntax variants into an older translation path; forgetting the 'u' flag for \p{L}-style classes; hand-written patterns with an unmatched '('.
Related errors
- Invalid RegExp literal:
- extract() needs three arguments: text, regex, group
- extractAll() needs three arguments: text, regex, group
- String.prototype.replaceAll called with a non-global RegExp…
- String.prototype.matchAll called with a non-global RegExp…
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/aedfc53585783b75.
Report an issue: GitHub.
Appendix: source
Thrown at karate-js/src/main/java/io/karatelabs/js/JsRegex.java:97
this.pattern = literalText.substring(1, lastSlashIndex);
this.flags = lastSlashIndex < literalText.length() - 1
? literalText.substring(lastSlashIndex + 1)
: "";
} else {
// string patterns without delimiters
this.pattern = literalText;
this.flags = "";
}
this.global = this.flags.contains("g");
this.sticky = this.flags.contains("y");
this.groupNames = extractGroupNames(this.pattern);
int javaFlags = translateJsFlags(this.flags);
try {
// unescape js-specific regex syntax that differs from Java
String javaPattern = translateJsRegexToJava(this.pattern);
this.javaPattern = Pattern.compile(javaPattern, javaFlags);
} catch (PatternSyntaxException e) {
throw JsErrorException.syntaxError("Invalid regular expression: /" + pattern + "/ - " + e.getMessage());
}
}
JsRegex(String pattern, String flags) {
super(null, JsRegexPrototype.INSTANCE);
this.pattern = pattern;
this.flags = flags != null ? flags : "";
this.global = this.flags.contains("g");
this.sticky = this.flags.contains("y");
this.groupNames = extractGroupNames(this.pattern);
int javaFlags = translateJsFlags(this.flags);
try {
String javaPattern = translateJsRegexToJava(this.pattern);
this.javaPattern = Pattern.compile(javaPattern, javaFlags);
} catch (PatternSyntaxException e) {
throw JsErrorException.syntaxError("Invalid regular expression: /" + pattern + "/ - " + e.getMessage());
}
}View on GitHub (pinned to a22eb90246)