diff --git a/src/platform/packages/shared/kbn-esql-ast/src/parser/__tests__/literal.test.ts b/src/platform/packages/shared/kbn-esql-ast/src/parser/__tests__/literal.test.ts index 71ae3603c9c20..356daf8aa43ce 100644 --- a/src/platform/packages/shared/kbn-esql-ast/src/parser/__tests__/literal.test.ts +++ b/src/platform/packages/shared/kbn-esql-ast/src/parser/__tests__/literal.test.ts @@ -116,6 +116,42 @@ describe('literal expression', () => { ], }); }); + + it('escape double-quote before backslash', () => { + const text = `ROW "a\\"\\\\b", 1`; + const { root } = parse(text); + + expect(root.commands[0]).toMatchObject({ + type: 'command', + args: [ + { + type: 'literal', + literalType: 'keyword', + name: '"a\\"\\\\b"', + valueUnquoted: 'a"\\b', + }, + {}, + ], + }); + }); + + it('escape backslash before double-quote', () => { + const text = `ROW "a\\\\\\"b", 1`; + const { root } = parse(text); + + expect(root.commands[0]).toMatchObject({ + type: 'command', + args: [ + { + type: 'literal', + literalType: 'keyword', + name: '"a\\\\\\"b"', + valueUnquoted: 'a\\"b', + }, + {}, + ], + }); + }); }); describe('triple quoted', () => { diff --git a/src/platform/packages/shared/kbn-esql-ast/src/parser/factories.ts b/src/platform/packages/shared/kbn-esql-ast/src/parser/factories.ts index 0db09c0f9dfa7..735c80dc26283 100644 --- a/src/platform/packages/shared/kbn-esql-ast/src/parser/factories.ts +++ b/src/platform/packages/shared/kbn-esql-ast/src/parser/factories.ts @@ -127,11 +127,11 @@ export function createLiteralString(ctx: StringContext): ESQLLiteral { if (!isTripleQuoted) { valueUnquoted = valueUnquoted - .replace(/\\\\/g, '\\') .replace(/\\"/g, '"') .replace(/\\r/g, '\r') .replace(/\\n/g, '\n') - .replace(/\\t/g, '\t'); + .replace(/\\t/g, '\t') + .replace(/\\\\/g, '\\'); } return Builder.expression.literal.string(