A library of common Regular Expression patterns for parsing JSON, code blocks, and other structures from LLM outputs.
Finds and extracts a JSON object (curly braces) from a string.
\{[^{}]*(?:\{[^{}]*\}[^{}]*)*\}Here is the result: {"name": "John", "age": 30}.Finds and extracts a JSON array (brackets) from a string.
\[[^\[\]]*(?:\[[^\[\]]*\][^\[\]]*)*\]The output is: ["apple", "banana"].Extracts content from Markdown fenced code blocks (```...```).
```(?:[a-zA-Z0-9]*)?\n([\s\S]*?)\n``````javascript\nconsole.log("Hello");\n```Finds a standard email address pattern.
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}Contact us at support@example.com for help.Finds HTTP/HTTPS URLs.
https?:\/\/[^\s]+Visit https://example.com/page for more info.Extracts content between double quotes.
"([^"]*)"The answer is "42".LLMs do not always return cleanly parseable data. Sometimes they add conversational preambles ("Here is the JSON:") or extra text after the structured output. Regex helps you extract just the part you need.
For simple flat JSON, yes. For deeply nested or complex JSON, Regex can be fragile. A safer approach is to use Regex to isolate the block, then use JSON.parse() for actual parsing in a try/catch.
const match = text.match(/YOUR_REGEX/g);. The /g flag returns all matches.
Copy example regular expressions for common extraction tasks.