From b8fcc394e57daddd595f91425cf98a76490ad464 Mon Sep 17 00:00:00 2001 From: Chaitanya Date: Tue, 23 Dec 2025 10:19:51 +0530 Subject: [PATCH] fix: handle escaped backslashes correctly when parsing JSON strings --- lib/getPartsOfJson.ts | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/getPartsOfJson.ts b/lib/getPartsOfJson.ts index d8b741ccb..138a49be7 100644 --- a/lib/getPartsOfJson.ts +++ b/lib/getPartsOfJson.ts @@ -9,9 +9,21 @@ const regexNumber = /^\s*(?-?\d+(\.\d+)?([Ee][+-]?\d+)?)\s*$/g; const regexString = /^\s*(?"(\\"|[^"])*")\s*$/g; const regexBoolean = /^\s*(?true|false)\s*$/g; const regexNull = /^\s*(?null)\s*$/g; -const regexDoubleQuote = /(? { + let backslashCount = 0; + let i = quoteIndex - 1; + + while (i >= 0 && str[i] === '\\') { + backslashCount++; + i--; + } + + return backslashCount % 2 === 1; +}; + export type SyntaxPart = { type: string; address?: string; @@ -188,10 +200,17 @@ const getPartsOfJsonObjectContent = ( offset = 0, jsonPath: string, ): SyntaxPart[] => { - const doubleQuoteMatches = getFindResultsByGlobalRegExp( - serializedJson, - regexDoubleQuote, - ); + const doubleQuoteMatches: RegExpResult[] = []; + + for (let i = 0; i < serializedJson.length; i++) { + if (serializedJson[i] === '"' && !isEscapedQuote(serializedJson, i)) { + doubleQuoteMatches.push({ + index: i, + match: '"', + groups: [], + }); + } + } let keywordStartIndex = 0; let stringsWithPayload: StringWithPayload[] = []; doubleQuoteMatches.forEach((doubleQuoteMatch, index) => {