INNER CODE UNIT · TypeScript
extraNewLinesRegex
austin-starks/Promptimizer · src/utils/index.ts:25
const extraNewLinesRegex = /\n\s*\n/g;
result = result.replace(extraNewLinesRegex, "\n");
return result.trim();
} catch (e) {
return text;
}
}
export function extractJson(text: string): Record<string, any> | undefined {
if (!text) {
return undefined;
}
// Use regex to find the JSON content within the text
const jsonMatch = text.match(/{[\s\S]*}/);
if (!jsonMatch) {
return undefined;
}