Regex Generator

Choose what you want to validate, such as an email address, a phone number, a date or a postcode, or set your own rules for allowed characters and length, and get a ready regular expression. The tool explains each part in plain English, gives you the JavaScript line and the HTML pattern attribute to paste in, and lets you test a sample straight away. It builds from a library of checked patterns and a rule builder, so you do not need to know the syntax.

What you enter

  • What to match
  • Match the whole text only (add ^ and $)
  • Lower-case letters a to z
  • Upper-case letters A to Z
  • Digits 0 to 9
  • Spaces
  • Underscore
  • Hyphen
  • Dot
  • Other allowed characters
  • Must start with a letter
  • Minimum length
  • Maximum length (leave empty for no limit)
  • Require a lower-case letter
  • Require an upper-case letter
  • Require a digit
  • Require a symbol
  • Sample to test (optional)

What you get

  • Pattern
  • Sample result

How it works

  1. Pick a pattern type from the list. Choose Custom rules to build your own from allowed characters and a length, or Password rules for a length plus required character types.
  2. Decide whether the pattern must match the whole text (anchors on) or may match anywhere inside longer text (anchors off).
  3. Type a sample into the test box to see whether it matches.
  4. Copy the pattern, the JavaScript line or the HTML input with the pattern attribute. The breakdown table shows what each piece does.

How the pieces fit together

Most patterns are built from the same four ideas. A character class lists which characters are allowed in one position, such as [a-z0-9]. A quantifier says how many of them, such as {3,16} for three to sixteen. Groups with a question mark, such as (?:-\d{4})?, make a part optional. Anchors fix where the match must start and end.

Reading the breakdown table beside the pattern is a good way to learn the notation. Each row is one piece of the pattern with its meaning, from left to right, so you can see how the whole thing is assembled and adapt it by hand.

Choosing between strict and forgiving

A strict pattern rejects more bad input but also rejects more good input. For something with a published standard, such as a hex colour or an IPv4 address, strict is right because the format is exact. For something free-form, such as a phone number or a name, forgiving is better: accept spaces, brackets and dashes, then clean the value in code.

Think about what a failure costs. A rejected real person leaves your form; an accepted bad value ends up in your database. If you can clean up bad values later, accept more at the door. If bad values cause harm, such as a malformed value used in a command or a query, be strict and validate again on the server.

Tips

  • Use a pattern to check the shape of input, then confirm it another way. A correct email pattern cannot tell you whether the mailbox exists. Send a confirmation message for that.
  • Do not make email patterns stricter than the standard allows. Rejecting a real customer because of a plus sign or a long ending costs you a sale.
  • Date patterns check the format and the ranges of the day and month, but not whether the day exists in that month. 2025-02-30 passes the pattern, so check the calendar in code.
  • For HTML forms, the pattern attribute is always matched against the whole value, so the anchors are not needed there. The tool leaves them out of the HTML line.
  • Always validate again on the server. Browser checks improve the form, but anyone can bypass them.

Frequently asked questions

What is a regular expression?

A regular expression, or regex, is a compact pattern that describes a set of text strings. Programs use it to check input, find text and replace text. For example, ^\d{5}$ describes exactly five digits, such as a short ZIP code.

What does the anchors option do?

With anchors on, the pattern starts with ^ and ends with $, so the whole text must fit the pattern. With anchors off, the pattern can match a piece inside longer text, which is what you want when searching a paragraph for emails or colours.

Why is the HTML pattern shorter than the JavaScript one?

The browser treats an HTML pattern attribute as if it were wrapped in ^(?: and )$, so it always has to match the whole value. The anchors would be redundant, and the tool leaves them out.

How does the custom builder write the length?

It writes a quantifier after the character class. A minimum of 3 and a maximum of 16 gives {3,16}; a minimum of 1 with no maximum gives +; equal minimum and maximum give an exact count such as {8}. If you tick must start with a letter, the first character gets its own class and the length counts include it.

Are the patterns guaranteed to be right for every country and case?

They are practical patterns for the usual formats, not complete legal definitions. Phone numbers, postcodes and addresses vary by country and change over time, so treat the result as a first filter and test it on your real data.

Related: Regex tester, JSON formatter and validator, Hex colour picker.

Start for free or see all free tools. We.Inc is free to start, with no card needed. AI building is on paid plans from $20 a month.

More in Free Tools

Product

Who It's For

Features

Resources

Company

View Sitemap