Regex Tester & DebuggerSpecialized Version
🔍

Regex Extractor

Extract with regex

//g
Flags:
Examples:

Regex Extractor

Extract matching text from content using regular expressions. Pull out emails, URLs, numbers, dates, and custom patterns from any text.

Basic Extraction

``javascript // Extract all matches const text = "Contact: john@email.com or jane@company.org"; const emails = text.match(/[\w.-]+@[\w.-]+\.[a-z]{2,}/gi); // Result: ["john@email.com", "jane@company.org"] `

Extraction with Capture Groups

`javascript // Extract specific parts using groups function extractAll(text, pattern) { const regex = new RegExp(pattern, 'g'); const matches = []; let match;

while ((match = regex.exec(text)) !== null) { matches.push({ full: match[0], groups: match.slice(1), named: match.groups || {} }); }

return matches; }

// Extract URLs with protocol and domain const urlPattern = /(https?):\/\/([\w.-]+)/g; const urls = extractAll("Visit https://google.com or http://example.org", urlPattern); // Result: [ // { full: "https://google.com", groups: ["https", "google.com"] }, // { full: "http://example.org", groups: ["http", "example.org"] } // ] `

Named Capture Groups

`javascript // Modern JavaScript named groups const datePattern = /(?\d{4})-(?\d{2})-(?\d{2})/g; const text = "Events: 2024-12-25, 2025-01-01";

let match; while ((match = datePattern.exec(text)) !== null) { console.log(Year: ${match.groups.year}, Month: ${match.groups.month}); } // Year: 2024, Month: 12 // Year: 2025, Month: 01 `

Common Extraction Patterns

| Data Type | Pattern | Example Match | Email[\w.-]+@[\w.-]+\.[a-z]{2,}user@email.com URLhttps?://[\w.-]+(?:/[\w./-]*)?https://example.com/path Phone\+?\d{1,3}[-.\s]?\d{3}[-.\s]?\d{3}[-.\s]?\d{4}+1-555-123-4567 IP Address\d{1,3}(?:\.\d{1,3}){3}192.168.1.1 Date\d{4}-\d{2}-\d{2}2024-12-25 Time\d{2}:\d{2}(?::\d{2})?14:30:00 Hashtag#\w+#coding @mention@\w+@username Money\$[\d]+(?:\.\d{2})?$1,234.56 | Hex color | #[0-9a-fA-F]{6}\b | #FF5733 |

Extract and Transform

`javascript // Extract numbers and calculate const prices = "Items: $10, $25.50, $100"; const amounts = prices.match(/\$(\d+(?:\.\d{2})?)/g) .map(p => parseFloat(p.replace('$', ''))); const total = amounts.reduce((a, b) => a + b, 0); // amounts: [10, 25.50, 100], total: 135.50 ``

Frequently Asked Questions

How do I extract just part of the match?

Use capture groups with parentheses. Pattern (\\d+)-(\\d+) on "123-456" gives match[0]="123-456", match[1]="123", match[2]="456". Access groups via match.slice(1) or named groups with (?<name>pattern) and match.groups.name.

Why does match() return null instead of an empty array?

JavaScript's match() returns null when no matches are found, not an empty array. Always check: const matches = text.match(pattern) || []. Or use matchAll() which returns an iterator (never null): [...text.matchAll(pattern)].

How do I extract the last match?

Get all matches and take the last: const matches = text.match(/pattern/g); const last = matches?.[matches.length - 1]. Or use negative lookahead to match only the last occurrence: /pattern(?!.*pattern)/

Related Tools

Explore other tools you might find useful:

Related Calculators