{"record":{"id":"07fda41f90ab9b14","repo":"karatelabs/karate","slug":"invalid-regexp-literal","errorCode":null,"errorMessage":"Invalid RegExp literal: ","messagePattern":"Invalid RegExp literal: ","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsRegex.java","lineNumber":76,"sourceCode":"        lastIndex = 0;\n    }\n\n    // @@matchAll clones matching state — the iterator starts from the\n    // receiver's current position without ever writing it back.\n    int currentLastIndex() {\n        return lastIndex;\n    }\n\n    JsRegex() {\n        this(\"(?:)\");\n    }\n\n    JsRegex(String literalText) {\n        super(null, JsRegexPrototype.INSTANCE);\n        if (literalText.startsWith(\"/\")) {\n            int lastSlashIndex = literalText.lastIndexOf('/');\n            if (lastSlashIndex <= 0) {\n                throw JsErrorException.syntaxError(\"Invalid RegExp literal: \" + literalText);\n            }\n            // extract pattern and flags from the literal\n            this.pattern = literalText.substring(1, lastSlashIndex);\n            this.flags = lastSlashIndex < literalText.length() - 1\n                    ? literalText.substring(lastSlashIndex + 1)\n                    : \"\";\n        } else {\n            // string patterns without delimiters\n            this.pattern = literalText;\n            this.flags = \"\";\n        }\n        this.global = this.flags.contains(\"g\");\n        this.sticky = this.flags.contains(\"y\");\n        this.groupNames = extractGroupNames(this.pattern);\n        int javaFlags = translateJsFlags(this.flags);\n        try {\n            // unescape js-specific regex syntax that differs from Java\n            String javaPattern = translateJsRegexToJava(this.pattern);","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsRegex.java#L58-L94","documentation":"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.","triggerScenarios":"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.","commonSituations":"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 '/'.","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"],"exampleFix":"// before\nJsRegex r = new JsRegex(\"/abc\"); // missing closing slash\n// after\nJsRegex r = new JsRegex(\"/abc/\"); // complete literal","handlingStrategy":"validation","validationCode":"function isValidRegexLiteral(s) { return typeof s === 'string' && s.startsWith('/') && s.lastIndexOf('/') > 0; }\nif (!isValidRegexLiteral(src)) throw new Error('malformed regex literal: ' + src);","typeGuard":null,"tryCatchPattern":"try {\n  const re = new JsRegex(literal);\n} catch (JsErrorException e) {\n  if (String(e.getMessage()).startsWith('Invalid RegExp literal')) {\n    throw new IllegalArgumentException('regex literal missing slashes: ' + literal);\n  }\n  throw e;\n}","preventionTips":["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"],"tags":["javascript","regex","syntaxerror","regex-literal"],"backgroundTag":"invalid-regex-pattern","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}