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 |
|---|---|---|
[\w.-]+@[\w.-]+\.[a-z]{2,} | user@email.com | |
| URL | https?://[\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
``