Regex Tester & DebuggerSpecialized Version
🔍

Regex Generator

Generate regex

//g
Flags:
Examples:

Regex Generator

Generate regular expressions from example patterns or descriptions. Build complex regex patterns step-by-step without memorizing syntax.

Building Regex Step by Step

NeedPatternExplanation
Phone number\d{3}-\d{3}-\d{4}3 digits, dash, 3 digits, dash, 4 digits
Email[\w.-]+@[\w.-]+\.[a-z]{2,}word chars, @, domain, TLD
Date\d{4}-\d{2}-\d{2}YYYY-MM-DD format
Time\d{2}:\d{2}(:\d{2})?HH:MM or HH:MM:SS
ZIP code\d{5}(-\d{4})?5 digits, optional +4

Regex Generator Helpers

``javascript const regexBuilder = { // Common patterns digit: '\\d', digits: (n) => \\d{${n}}, letter: '[a-zA-Z]', letters: (n) => [a-zA-Z]{${n}}, word: '\\w+', whitespace: '\\s+',

// Quantifiers optional: (p) => (${p})?, oneOrMore: (p) => (${p})+, zeroOrMore: (p) => (${p})*, exactly: (p, n) => (${p}){${n}}, between: (p, min, max) => (${p}){${min},${max}},

// Structure group: (p) => (${p}), namedGroup: (name, p) => (?<${name}>${p}), nonCapturing: (p) => (?:${p}), either: (...patterns) => (${patterns.join('|')}),

// Anchors startsWith: (p) => ^${p}, endsWith: (p) => ${p}$, exact: (p) => ^${p}$,

// Escape special characters escape: (str) => str.replace(/[.*+?^$\{}()|[\]\\]/g, '\\$&') };

// Build phone pattern const phoneRegex = regexBuilder.exact( regexBuilder.digits(3) + '-' + regexBuilder.digits(3) + '-' + regexBuilder.digits(4) ); // Result: ^\d{3}-\d{3}-\d{4}$ `

Common Pattern Templates

Pattern NameRegexMatches
US Phone\(\d{3}\) \d{3}-\d{4}(555) 123-4567
Credit Card\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}1234-5678-9012-3456
SSN\d{3}-\d{2}-\d{4}123-45-6789
UUID[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}UUID format
Hex color#[0-9a-fA-F]{6}\b#FF5733
IPv4\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}192.168.1.1

Capture Groups for Extraction

`javascript // Extract parts of a phone number const phonePattern = /\((\d{3})\) (\d{3})-(\d{4})/; const match = "(555) 123-4567".match(phonePattern); // match[1] = "555" (area code) // match[2] = "123" (exchange) // match[3] = "4567" (subscriber) ``

Frequently Asked Questions

How do I create a regex for multiple formats?

Use alternation with |. For phone numbers accepting 555-1234, (555) 123-4567, or 555.123.4567: \\d{3}[-.]?\\d{3}[-.]?\\d{4}|\\(\\d{3}\\)\\s?\\d{3}-\\d{4}. Or use optional groups: \\(?\\d{3}\\)?[-.\\s]?\\d{3}[-.\\s]?\\d{4}

Should I use regex for email validation?

For basic validation, a simple pattern works: ^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$. Perfect RFC-compliant email regex is extremely complex. For real validation, consider: 1) Basic regex for format check, 2) Send verification email for real validation. Don't reject unusual but valid emails.

How do I match text that spans multiple lines?

Use the s (dotall) flag to make . match newlines: /start.*end/s. Or use [\\s\\S] instead of dot: /start[\\s\\S]*end/. For line-by-line matching with ^ and $, use the m (multiline) flag: /^line/gm matches "line" at the start of each line.

Related Tools

Explore other tools you might find useful:

Related Calculators