{"record":{"id":"c6d737e297dac458","repo":"karatelabs/karate","slug":"invalid-hex-escape-sequence-in-template-literal","errorCode":null,"errorMessage":"invalid hex escape sequence in template literal","messagePattern":"invalid hex escape sequence in template literal","errorType":"exception","errorClass":"io.karatelabs.parser.ParserException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/parser/JsLexer.java","lineNumber":399,"sourceCode":"                advance();\n                if (isAtEnd()) {\n                    break;\n                }\n                char esc = peek();\n                // Spec: in non-tagged template literals, malformed escape sequences are\n                // early errors. We have no way to tell tag vs no-tag at lex time, so we\n                // reject at lex time; tagged templates are not yet supported anyway.\n                // Invalid legacy octal: \\1..\\9 or \\0 followed by a decimal digit.\n                if (esc >= '1' && esc <= '9') {\n                    throw new ParserException(\"invalid escape sequence in template literal: \\\\\" + esc);\n                }\n                if (esc == '0' && peek(1) >= '0' && peek(1) <= '9') {\n                    throw new ParserException(\"invalid escape sequence in template literal: \\\\0\" + peek(1));\n                }\n                // Hex escape backslash-x HH — require exactly two hex digits.\n                if (esc == 'x') {\n                    if (!isHexDigit(peek(1)) || !isHexDigit(peek(2))) {\n                        throw new ParserException(\"invalid hex escape sequence in template literal\");\n                    }\n                }\n                // Unicode escape backslash-u HHHH or backslash-u {H...} — require proper form.\n                if (esc == 'u') {\n                    char next = peek(1);\n                    if (next == '{') {\n                        // backslash-u { codepoint } — one or more hex digits, closed by }\n                        int j = 2;\n                        if (!isHexDigit(peek(j))) {\n                            throw new ParserException(\"invalid unicode escape sequence in template literal\");\n                        }\n                        int codepoint = 0;\n                        while (isHexDigit(peek(j))) {\n                            char h = peek(j);\n                            int v = (h >= '0' && h <= '9') ? h - '0'\n                                    : (h >= 'a' && h <= 'f') ? h - 'a' + 10\n                                    : h - 'A' + 10;\n                            codepoint = (codepoint << 4) | v;","sourceCodeStart":381,"sourceCodeEnd":417,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/parser/JsLexer.java#L381-L417","documentation":"\\x in a template literal must be followed by exactly two hex digits (\\xHH); anything else is an invalid hex escape and the lexer throws. This is an ES-spec early error for untagged templates, enforced at lex time by karate-js.","triggerScenarios":"Template literal contains \\x not followed by two hex digits, e.g. `\\x1`, `\\xg`, `\\x`, or a Windows path/regex fragment like `\\xml` inside backticks; thrown at JsLexer.java:399.","commonSituations":"Half-written hex escapes, file extensions or tags after a backslash (\\xml, \\xlsx), copy-pasted snippets from languages with different escape rules, regex patterns pasted into template strings.","solutions":["Pad the hex escape to two digits: \\x0a instead of \\xa or \\x1","Escape the backslash if you meant a literal one: `\\\\xml`","Use \\u00XX form if you need a single-digit codepoint","Verify the intended character code and rewrite the escape"],"exampleFix":"// before\nvar nl = `a\\xb`;        // only one hex digit\nvar tag = `\\xml data`;  // \\x followed by 'm'\n// after\nvar nl = `a\\x0b`;\nvar tag = `\\\\xml data`;","handlingStrategy":"validation","validationCode":"// ensure every \\x is followed by exactly two hex digits\nfunction hasBadHexEscape(tpl) {\n  var m = tpl.match(/\\\\x[0-9a-fA-F]{0,2}/g) || [];\n  return m.some(function (s) { return s.length !== 4; }) || /\\\\x(?![0-9a-fA-F]{2})/.test(tpl);\n}\nif (hasBadHexEscape(tpl)) throw new Error('bad \\\\x escape in template literal');","typeGuard":"null","tryCatchPattern":"try {\n  karate.eval('var s = `' + tpl + '`;');\n} catch (e) {\n  if (String(e.message).indexOf('invalid hex escape') >= 0) {\n    tpl = tpl.replace(/\\\\x/g, '\\\\u00'); // promote to \\u00NN form where possible\n  } else throw e;\n}","preventionTips":["Always zero-pad hex escapes to two digits (\\x0a not \\xa)","Double backslashes for literal text like \\xml","Validate escapes with a regex before dynamic eval","Prefer \\uXXXX uniformly over \\xHH"],"tags":["javascript","lexer","template-literal","escape-sequence"],"backgroundTag":"invalid-escape-sequence","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"}