const removeCommentsRegex = /\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g; /** * if text isn't json, returns null */ export const tryParseJson = ( text: string, logParseError?: boolean, ): T | null => { try { const jsonStringWithoutComments = text.replace( removeCommentsRegex, (m, g) => (g ? "" : m), ); return JSON.parse(jsonStringWithoutComments) as T; } catch (parseError) { if (logParseError) console.log("JSON Parse error:", parseError); return null; } };