Regex Generator: Common Patterns and Flags

Developer Tools Active

Choose one of eight presets and a flag set to produce a copyable JavaScript regular expression literal for common text formats.

Regex Generator

⚡ Regex Generator

Choose a preset and a flag set to get a JavaScript regex literal for emails, URLs, dates, US phone numbers, IPv4 addresses, hex colors, slugs and alphanumeric text.

Preset Library
Pattern Type
Email Address
Expression Flags
None
Status
Valid

Rate This Tool

Your feedback helps others discover great tools

–
out of 5
No ratings yet

Tap a star to rate:

About This Tool

Need a regex for an email address, a web link, or a date? A regex generator gives you a working starting pattern without writing one from scratch. This Regex Generator returns one of eight fixed presets as a JavaScript regex literal.

It suits web developers, students, and anyone who checks form fields or searches text. You pick a preset and a flag set, and the expression refreshes right away.

Table of Contents

  • What the Regex Generator Does
  • How to Use It, Step by Step
  • How the Expression Is Built
  • Worked Examples
  • How to Read the Results
  • Tips for Using the Output
  • Limitations and Assumptions
  • FAQs About the Regex Generator

What the Regex Generator Does

Each preset is a regular expression built for one common text format. The output appears in a read-only field, ready to copy.

  • Email Address: a dot-separated local part, an at sign, a domain, and a top level domain.
  • URL / Hyperlink: finds an http or https address inside longer text.
  • US Phone Number: ten digits, an optional +1 or 1 prefix, and optional area code parentheses.
  • IPv4 Address: four numbers from 0 to 255, separated by dots, with no leading zeros.
  • Date (YYYY-MM-DD): a four digit year, then a month and day that exist on the calendar.
  • Hex Color Code (# optional): three or six hex digits, with an optional leading hash.
  • URL Slug: lowercase letters and digits, with single hyphens between words.
  • Alphanumeric String: letters and digits only, with no spaces or symbols.

The Flags list offers six choices: No flags (the default), g, i, m, gi, and gm. Each letter changes how the expression behaves, as the tips below explain.

How to Use It, Step by Step

  1. Open the Select Target Data Preset list and choose a format, such as Email Address.
  2. Open the Flags list and choose an option, such as No flags or i.
  3. Press Generate Expression. Changing either list also refreshes the output.
  4. Read the Generated Regular Expression field. It shows the full literal, with slashes and flags.
  5. Press Copy Regex, then read the message under the field.
  6. Paste the expression into your code or a tester, then check it with your own sample text.

How the Expression Is Built

The tool looks up the preset pattern and places it between two slashes. It then adds the flag letters after the closing slash. With No flags, nothing follows the closing slash.

Preset Pattern+Slashes+Flags=Regex Literal
  • The preset pattern is fixed text stored in the page for each format.
  • The slashes wrap the pattern, so the output reads as a JavaScript regex literal.

The tool does not run the pattern against any text. It only checks that the literal compiles, so the output is only as strict as its preset.

Worked Examples

The table lists outputs from the tool's current code. Each row was run through the same JavaScript regex engine that browsers use.

PresetFlagsGenerated ExpressionTest InputResult
Email Address (default on load)None/^[a-zA-Z0-9_%+-]+(?:\.[a-zA-Z0-9_%+-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/priya.shah@example.comMatch
Email AddressNone/^[a-zA-Z0-9_%+-]+(?:\.[a-zA-Z0-9_%+-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/a..b@x.comNo match
US Phone NumberNone/^(?:\+?1[-.\s]?)?(?:\(\d{3}\)|\d{3})[-.\s]?\d{3}[-.\s]?\d{4}$/(555) 123-4567Match
US Phone NumberNone/^(?:\+?1[-.\s]?)?(?:\(\d{3}\)|\d{3})[-.\s]?\d{3}[-.\s]?\d{4}$/12345No match
Date (YYYY-MM-DD)None/^(?:(?:\d{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29|\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\d|30)|02-(?:0[1-9]|1\d|2[0-8])))$/2026-02-31No match
Date (YYYY-MM-DD)None/^(?:(?:\d{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29|\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\d|30)|02-(?:0[1-9]|1\d|2[0-8])))$/2024-02-29Match
URL SlugNone/^[a-z0-9]+(?:-[a-z0-9]+)*$/Hello-WorldNo match
URL Slugi/^[a-z0-9]+(?:-[a-z0-9]+)*$/iHello-WorldMatch

The phone rows show that the parenthesis format matches and a 5-digit run does not. The date rows show that February 31 fails, while February 29 passes in leap years.

How to Read the Results

Each card updates after every run. The output field below them holds the expression you copy.

  • Pattern Type: the name of the selected preset, such as Email Address.
  • Expression Flags: the flag letters in the output, or None when no flags are set.
  • Status: Valid when the expression compiles, and Invalid when it does not. It does not test your text.
  • Generated Regular Expression: the read-only literal, ready for the Copy Regex button.

A match means the expression accepts the test string. It does not prove the format is correct everywhere. Check important cases with several sample strings, including ones that should fail.

Tips for Using the Output

The g flag keeps a position between test() calls. Repeated calls on one literal can alternate between true and false. The tool shows a warning when you choose g, so remove it for single checks.

The i flag ignores letter case, so the URL Slug preset accepts Hello-World. Slugs are normally lowercase, so keep No flags when you need lowercase only.

The m flag makes ^ and $ match at every line break. A multi-line string can pass a test with m that fails without it.

The URL preset does not anchor its pattern, so it finds a link anywhere in a longer sentence. That suits search tasks, but full-field checks need your own anchors in code. Paste the output into the Regex Tester to see matches in real text.

When you replace text instead of checking it, use the Regex Replace Tester. The Regex Cheat Sheet is useful when you forget a symbol. Escape user text with the Regex Escape Tool before you join it to a pattern.

For other developer utilities, browse the Developer Tools hub.

Limitations and Assumptions

  • The tool offers eight fixed presets only. It cannot build a custom pattern from your input.
  • The page has no test field. Check each expression with your own sample text.
  • The US Phone Number preset checks shape only. It does not confirm that area or exchange codes are assigned.
  • The Date preset applies the leap-year rule for years 1600 to 2400. It does not handle year 0000 or calendar reform dates.
  • A preset match is not a guarantee of validity in every parser or programming language.

FAQs About the Regex Generator

Can I Use the Output in Python?

The output uses JavaScript syntax, so Python needs changes. Remove the slashes and the g. Then pass i as re.IGNORECASE and m as re.MULTILINE.

Why Does the Date Preset Reject 1900-02-29?

Years divisible by 4 are leap years, except century years, which need divisibility by 400. The preset applies that Gregorian rule, so 2000-02-29 passes while 1900-02-29 fails.

Should I Use a Regex to Validate Email Addresses?

Only as a first filter. The Email preset checks the shape of an address, so it cannot show that a mailbox exists. It also accepts a leading hyphen in the local part, such as -user@example.com, because RFC 5322 allows that character.

Does the Date Preset Accept Single-Digit Months?

No. The month and day must each use two digits, so write 2026-2-5 as 2026-02-05 before you test it. The preset then accepts the date.

Why Is the Output Field Read Only?

The tool sets the field, so only the preset and flag lists can change it. To adjust the text, edit the copied expression in your own code.

What Happens When Copy Regex Fails?

The tool shows a message under the field that says the copy failed. Select the text in the output field and copy it by hand.

This calculator gives general estimates only and is not professional advice. Reviewed for general accuracy by Prof. Dr. Khalil Mudassar, PhD.

Category: Developer Tools

You May Also Like

View All