Regex Settings
Choose a pattern
Your output will appear here.
This free regex generator builds a regular expression for common patterns such as email addresses, URLs, US phone numbers and dates, or for custom text you type. Choose where the match must appear and which flags to use, test it on sample text, and copy code for JavaScript, Python and PHP. It runs in your browser with no sign-up.
Choose a pattern
Your output will appear here.
Pick email, URL, phone, date, time, hex color, IPv4, ZIP code, numbers, hashtags, usernames, slugs and more.
Type your own text and choose whether it must appear anywhere, at the start, at the end or as the whole string.
Choose global or ignore case flags, and add ^ and $ to match the whole string only.
Paste sample text in Test text to check the pattern, then copy code for JavaScript, Python and PHP.
Three steps from a blank form to a result you can use.
Pick what you want to match from the list, or choose Custom text and type the text.
Pick the Flags, choose where custom text must appear, and decide whether to match the whole string only.
Add Test text, select Generate Regex, check the result, then copy the pattern or code.
A regular expression, or regex, is a short pattern that describes text you want to find or validate. This generator writes common patterns for you, so you do not have to remember the syntax for an email address or a date.
| Option | Typical Use |
|---|---|
| Email address | Check the format of an email field, or find addresses in text |
| URL (http/https) | Find web links in text or validate a website field |
| US phone number | Validate or find US style phone numbers |
| Date (YYYY-MM-DD), Date (MM/DD/YYYY) | Check date formats in forms, logs and imports |
| Time (24h HH:MM) | Validate times such as 09:30 or 18:45 |
| Hex color | Find or validate CSS color codes |
| IPv4 address | Find IP addresses in server logs |
| US ZIP code | Validate postal codes in US address forms |
| Whole number, Decimal number | Check numeric input or pull numbers out of text |
| Hashtag, @mention | Extract tags and handles from social posts |
| Username (3-16 chars) | Enforce username length and characters at sign-up |
| URL slug | Check page slugs such as my-first-post |
| Strong password | Enforce password rules in a sign-up form |
| Whole word | Match a word on its own, not inside longer words |
| Custom text (type below) | Match your own text, such as a product code prefix |
Two settings change how a pattern behaves more than anything else. MDN's regular expression guide defines them like this:
We suggest this rule of thumb:
| Job | Flags | Whole String Only |
|---|---|---|
| Validate a form field (one value) | None or Ignore case (i) | Ticked |
| Find all matches in a document | Global (g) or Global, ignore case (gi) | Not ticked |
| Check if text starts with a code | None | Not ticked, use At the start |
Without anchors, a validation pattern for an email address can pass a value such as "call me at hello@example.com today," because it finds an email somewhere inside. Anchors stop that.
Example: this is an illustration of two common jobs.
Job 1, find every email in a block of text. Choose Email address, set Flags to Global (g), leave Match the whole string only unticked, and use test text such as "Contact: hello@example.com, support@onethreadapp.com." A global email pattern should find both addresses. If it finds only one, check that the g flag is set.
Job 2, check that invoice numbers start with INV-. Choose Custom text (type below), type INV- in Text to match, set Where must it appear? to At the start and Flags to None. Test it with these lines:
Testing values that should fail is as important as testing ones that should pass. Most regex bugs are patterns that accept too much.
The same pattern is written slightly differently in each language. The generator gives you code for all three, and these are the differences worth knowing:
| Language | How Patterns Are Written | Finding All Matches |
|---|---|---|
| JavaScript | Between slashes with flags after, such as /abc/gi | Use the g flag with match or matchAll |
| Python | As a raw string, such as r"abc", with flags like re.IGNORECASE | re.findall returns every match |
| PHP | Inside delimiters, such as '/abc/i', modifiers after the closing delimiter | preg_match_all, since PHP has no g modifier |
Python's documentation recommends raw strings for all but the simplest patterns, because backslashes are otherwise treated as string escapes. It also separates re.search, which finds a match anywhere, from re.fullmatch, which requires the whole string to match. PHP's manual notes that if the delimiter appears inside the pattern, you must escape it or pick another delimiter, such as #.
Quick answers about the Regex Generator.
Yes. It is free with no sign-up and no limit. Pick a pattern or type custom text, set the flags and anchors, add optional test text and select Generate Regex. The tool runs in your browser, so the text you paste into Test text is not saved to an account.
It offers 18 options: email address, URL, US phone number, two date formats, 24 hour time, hex color, IPv4 address, US ZIP code, whole number, decimal number, hashtag, @mention, username of 3 to 16 characters, URL slug, strong password, whole word, and custom text that you type yourself.
The g flag, short for global, finds every match in the text instead of stopping at the first. The i flag ignores case, so ABC and abc match the same pattern. Use g for searching or replacing in a document, and leave it off when you validate a single form value.
Tick it when the entire value must fit the pattern, such as a ZIP code or username field. It adds ^ at the start and $ at the end. Without it, the pattern can match part of the value, so a field containing extra words around a valid email could still pass.
Yes. The generator gives code for JavaScript, Python and PHP. The core pattern is the same, but the wrapping differs: JavaScript uses slashes and flags, Python uses a raw string with re functions, and PHP needs delimiters and uses preg_match or preg_match_all. Test the code in your own environment.
No. A regex only checks that the text looks like an email address, with the right parts in the right order. It cannot tell whether the mailbox exists or belongs to the person. To confirm an address, send a verification email with a link the user must click.
This free invoice generator turns your business details, client details and line items into a clean invoice...
Our JavaScript generator writes JavaScript code from a plain description of your project.
Enter your name, target role, experience level, key skills and a short work history, and the AI resume...
This free sales tax calculator adds sales tax to a pre-tax price or removes it from a tax-inclusive total.