karatelabs/karate · error · SyntaxError
Invalid RegExp literal:
Error message
Invalid RegExp literal:
What it means
A JsRegex built from a literal-style string must have the form /pattern/flags — it must start with '/' and contain at least one more '/' so a non-empty pattern region exists. If lastSlashIndex <= 0 (no closing slash, or only the leading slash), a SyntaxError 'Invalid RegExp literal: <text>' is thrown. This mirrors the browser behavior where malformed regex literals are syntax errors.
Solutions
- Ensure the literal string has both opening and closing slashes: /pattern/flags
- Check any code that slices/concatenates the literal for a dropped trailing slash
- Escape literal '/' inside the pattern as \/ so it is not mistaken for the terminator, and verify lastIndexOf('/') > 0
Example fix
// before
JsRegex r = new JsRegex("/abc"); // missing closing slash
// after
JsRegex r = new JsRegex("/abc/"); // complete literal Defensive patterns
Strategy: validation
Validate before calling
function isValidRegexLiteral(s) { return typeof s === 'string' && s.startsWith('/') && s.lastIndexOf('/') > 0; }
if (!isValidRegexLiteral(src)) throw new Error('malformed regex literal: ' + src); Try / catch
try {
const re = new JsRegex(literal);
} catch (JsErrorException e) {
if (String(e.getMessage()).startsWith('Invalid RegExp literal')) {
throw new IllegalArgumentException('regex literal missing slashes: ' + literal);
}
throw e;
} Prevention
- Always author literals as /pattern/flags with both slashes present
- Escape internal slashes as \/ when building literals programmatically
- Check string-slicing/concatenation code that assembles literals for dropped tails
- Lint regex strings read from config or env for balanced slashes
When it happens
Trigger: Passing strings like '/', '/flags' (no closing slash), or an unterminated literal that lost its trailing slash (e.g. from string slicing or config interpolation) to the JsRegex literal constructor.
Common situations: Building regex source strings by concatenation and dropping the closing slash; reading a pattern from config/env where the slash was escaped or stripped; hand-editing a regex literal and deleting the trailing '/'.
Related errors
- Invalid regular expression: /
- 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/07fda41f90ab9b14.
Report an issue: GitHub.
Appendix: source
Thrown at karate-js/src/main/java/io/karatelabs/js/JsRegex.java:76
lastIndex = 0;
}
// @@matchAll clones matching state — the iterator starts from the
// receiver's current position without ever writing it back.
int currentLastIndex() {
return lastIndex;
}
JsRegex() {
this("(?:)");
}
JsRegex(String literalText) {
super(null, JsRegexPrototype.INSTANCE);
if (literalText.startsWith("/")) {
int lastSlashIndex = literalText.lastIndexOf('/');
if (lastSlashIndex <= 0) {
throw JsErrorException.syntaxError("Invalid RegExp literal: " + literalText);
}
// extract pattern and flags from the literal
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);View on GitHub (pinned to a22eb90246)