karatelabs/karate · error · ParserException

invalid unicode escape sequence in template literal

Error message

invalid unicode escape sequence in template literal

What it means

A \u escape in a template literal must be either \uHHHH (four hex digits) or \u{H+} (braced codepoint with at least one hex digit). \u{ with no hex digit before the closing brace is invalid, so the lexer throws. The ES spec classifies this as an early error in untagged templates.

Solutions

  1. Provide a valid codepoint: `\u{1F600}` or `\u{41}` for the braced form
  2. Use exactly four hex digits: `\u0041` instead of `\u41`
  3. Escape the backslash if the \u is literal text: `\\u{}`
  4. If building dynamically, validate the hex string before interpolation and fall back to String.fromCodePoint(cp)

Example fix

// before
var ch = `\u{${cp}}`; // cp undefined/empty -> `\u{}`
// after
var ch = (cp && /^[0-9a-fA-F]+$/.test(cp)) ? `\u{${cp}}` : String.fromCodePoint(parseInt(cp || '0', 16) || 63);
Defensive patterns

Strategy: validation

Validate before calling

function badUnicodeEscape(tpl) {
  return /\\u(?![0-9a-fA-F]{4}|\{[0-9a-fA-F]+\})/.test(tpl);
}
if (badUnicodeEscape(tpl)) throw new Error('malformed \\u escape in template literal');

Type guard

null

Try / catch

try {
  karate.eval('var s = `' + tpl + '`;');
} catch (e) {
  if (String(e.message).indexOf('invalid unicode escape') >= 0) {
    tpl = tpl.replace(/\\u(?![0-9a-fA-F]{4|\{)/g, '\\\\u');
  } else throw e;
}

Prevention

When it happens

Trigger: Template literal contains \u{ or \u followed by fewer than four non-hex characters, e.g. `\u{}`, `\u{z}`, `\u12`, `\uabc` — hit at JsLexer.java:409 in scanTemplateContent.

Common situations: Interpolated codepoints like `\u{${cp}}` where cp failed to substitute or is empty, truncated four-digit escapes from generated code, regex \u patterns pasted into template strings, typo'd unicode escapes.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/502d0deb393462c1. Report an issue: GitHub.

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/parser/JsLexer.java:409

                    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");
                        }
                        int codepoint = 0;
                        while (isHexDigit(peek(j))) {
                            char h = peek(j);
                            int v = (h >= '0' && h <= '9') ? h - '0'
                                    : (h >= 'a' && h <= 'f') ? h - 'a' + 10
                                    : h - 'A' + 10;
                            codepoint = (codepoint << 4) | v;
                            if (codepoint > 0x10FFFF) {
                                throw new ParserException("unicode escape sequence out of range in template literal");
                            }
                            j++;
                        }
                        if (peek(j) != '}') {
                            throw new ParserException("invalid unicode escape sequence in template literal");
                        }
                    } else {
                        // backslash-u HHHH — four hex digits

View on GitHub (pinned to a22eb90246)