karatelabs/karate · error · io.karatelabs.parser.ParserException
invalid escape sequence in template literal: \
Error message
invalid escape sequence in template literal: \<esc>
What it means
In a template literal (backtick string), a backslash followed by digit 1-9 is an invalid legacy octal escape; the ES spec makes malformed escapes early errors in untagged templates, so JsLexer rejects them at lex time. Fix the escape or double-escape if you intended a literal backslash.
Solutions
- Remove the invalid escape or correct it (use \0 for NUL, or \x01 / \u0001 for codepoint 1)
- Escape the backslash itself: write \\1 in the template literal to mean backslash followed by 1
- Move regex-replacement patterns into actual RegExp context instead of a template literal
- Use a plain single-quoted string with proper escaping if no interpolation is needed
Example fix
// before var s = `match group: \1`; // after var s = `match group: \\1`; // or use a real regex replacement context
Defensive patterns
Strategy: validation
Validate before calling
// reject invalid template escapes before handing the string to eval
function hasBadTemplateEscapes(tl) {
return /\\[1-9]/.test(tl) || /\\0[0-9]/.test(tl);
}
if (hasBadTemplateEscapes(tpl)) throw new Error('invalid legacy octal escape in template literal'); Type guard
null
Try / catch
try {
karate.eval('var s = `' + tpl + '`;');
} catch (e) {
if (String(e.message).indexOf('invalid escape sequence in template literal') >= 0) {
tpl = tpl.replace(/\\/g, '\\\\'); // escape all backslashes and retry
} else throw e;
} Prevention
- Never paste regex replacement patterns (\1, $1) into template literals
- Double backslashes when you mean a literal backslash in any JS string
- Prefer \xNN / \uNNNN forms over legacy octal escapes
- Lint templates with a JS parser before dynamic evaluation
When it happens
Trigger: A non-tagged template literal in JS evaluated by karate-js contains an escape like \1 through \9, e.g. `col \1` or a regex fragment pasted into a template string; hit in scanTemplateContent (JsLexer.java:391).
Common situations: Copy-pasting regex replacement patterns ($1, \1) into template literals, Windows path fragments like `C:\temp\2`, porting old octal escapes from other languages, string-building code that interpolates numbers right after a backslash.
Related errors
- invalid escape sequence in template literal: \0
- invalid hex escape sequence in template literal
- invalid unicode escape sequence in template literal
- unicode escape sequence out of range in template literal
- invalid numeric separator
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/a6e7d5aae64ac516.
Report an issue: GitHub.
Appendix: source
Thrown at karate-js/src/main/java/io/karatelabs/parser/JsLexer.java:391
c = peek();
if (c == '`') {
break;
}
if (c == '$' && peek(1) == '{') {
break;
}
if (c == '\\') {
advance();
if (isAtEnd()) {
break;
}
char esc = peek();
// Spec: in non-tagged template literals, malformed escape sequences are
// early errors. We have no way to tell tag vs no-tag at lex time, so we
// reject at lex time; tagged templates are not yet supported anyway.
// Invalid legacy octal: \1..\9 or \0 followed by a decimal digit.
if (esc >= '1' && esc <= '9') {
throw new ParserException("invalid escape sequence in template literal: \\" + esc);
}
if (esc == '0' && peek(1) >= '0' && peek(1) <= '9') {
throw new ParserException("invalid escape sequence in template literal: \\0" + peek(1));
}
// Hex escape backslash-x HH — require exactly two hex digits.
if (esc == 'x') {
if (!isHexDigit(peek(1)) || !isHexDigit(peek(2))) {
throw new ParserException("invalid hex escape sequence in template literal");
}
}
// Unicode escape backslash-u HHHH or backslash-u {H...} — require proper form.
if (esc == 'u') {
char next = peek(1);
if (next == '{') {
// backslash-u { codepoint } — one or more hex digits, closed by }
int j = 2;
if (!isHexDigit(peek(j))) {
throw new ParserException("invalid unicode escape sequence in template literal");View on GitHub (pinned to a22eb90246)