Back to Tools

Regex Generator for AI

A library of common Regular Expression patterns for parsing JSON, code blocks, and other structures from LLM outputs.

Extract JSON Object

JavaScript

Finds and extracts a JSON object (curly braces) from a string.

\{[^{}]*(?:\{[^{}]*\}[^{}]*)*\}
Example: Here is the result: {"name": "John", "age": 30}.

Extract JSON Array

JavaScript

Finds and extracts a JSON array (brackets) from a string.

\[[^\[\]]*(?:\[[^\[\]]*\][^\[\]]*)*\]
Example: The output is: ["apple", "banana"].

Extract Code Block

JavaScript

Extracts content from Markdown fenced code blocks (```...```).

```(?:[a-zA-Z0-9]*)?\n([\s\S]*?)\n```
Example: ```javascript\nconsole.log("Hello");\n```

Extract Email Address

Universal

Finds a standard email address pattern.

[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
Example: Contact us at support@example.com for help.

Extract URL

Universal

Finds HTTP/HTTPS URLs.

https?:\/\/[^\s]+
Example: Visit https://example.com/page for more info.

Extract Text Inside Quotes

Universal

Extracts content between double quotes.

"([^"]*)"
Example: The answer is "42".

Frequently Asked Questions

Why use Regex on AI output?

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.

Is Regex reliable for JSON?

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.

How do I use these in JavaScript?

const match = text.match(/YOUR_REGEX/g);. The /g flag returns all matches.