Verystep Guides Verystep Guides

Regex Basics: Read and Write Patterns That Actually Match

A regex looks like a cat walked across the keyboard — until you read it one small piece at a time.

Published November 7, 2026 - A Verystep guide

Illustration of a large magnifying glass over a grid of rounded slate tiles, with a few tiles highlighted in indigo and amber

A regular expression describes the shape of text, not one example of it. Literals match themselves, a dot matches almost any single character, and classes narrow the field: \d is a digit, \w a word character, \s whitespace, and [^\s@] anything except whitespace or @. Quantifiers set the count — + is one or more, {4} is exactly four — and they are greedy unless a ? makes them lazy. Anchors ^ and $ pin a match to the whole string, and parentheses group and capture. Shape is all a pattern can check: it cannot prove an email exists or a date is real.

A pattern describes the shape, not one example

A regular expression isn't an example of the text you want. It's a description of its shape. Paste a whole email address into a pattern box and you've told the engine to find that exact address. Write the shape instead — word characters, an @, more word characters, a dot, a short ending — and the same pattern finds thousands of addresses you've never seen.

Read a pattern left to right, one piece at a time. Each piece answers a small question: what kind of character can sit here, and how many? That's it. A scary-looking pattern is usually just a dozen tiny answers strung together.

Illustration of rows of rounded slate blocks with matching blocks highlighted in indigo and one amber block
A pattern lights up every block that fits the shape.

One warning: regex can't tell a real date from a string that merely looks like one. It checks shape. Only shape.

The pieces: literals, dots and character classes

Start plain. A literal character matches itself. The pattern cat finds c, a and t, in that order. Most of a real pattern is literals — the @ in an email check, the dashes in a date.

The dot is broader than most people expect. . matches any single character except a line break. So if you want an actual full stop, you can't just type a dot. You escape it — \. — and the backslash turns the special meaning off. That rule runs both ways: a backslash makes a special character ordinary, and some ordinary letters special.

Those letters are worth memorising. \d is a digit, \w is a word character (letters, digits and underscore), and \s is whitespace. Their capitals do the opposite: \D, \W and \S match anything that isn't a digit, word character or whitespace.

Square brackets build a class of your own. [abc] matches one character — a, b or c. A dash gives a range, so [a-z] is one lowercase letter. Put a caret after the opening bracket and the class flips: [^\s@] is one character that is neither whitespace nor @ — a handy way to say 'a chunk of an email address'.

How many? Quantifiers, and why they get greedy

A class or literal matches exactly once. Quantifiers change the count. ? is zero or one — optional. * is zero or more, and + is one or more. Braces give exact control: {4} is exactly four, {2,4} is two to four, and {2,} is two or more.

Placement trips people up. A quantifier applies only to the piece right before it. \d{4} is four digits. ab+ is an a followed by one or more b's — the plus doesn't touch the a. To count a whole run, group it first; that's a later section's job.

Quantifiers are greedy by default. Given .+ between two quotes, the engine won't stop at the first closing quote. It'll grab as much as it can, then backtrack just enough to let the rest succeed — often swallowing several quoted chunks as one match. Add a question mark and it turns lazy: .+? takes as little as it can. If a match looks too long, greed is the first suspect.

Where it may match: anchors, boundaries and flags

By default a pattern can match anywhere. \d+ happily matches the 123 inside abc123, because nothing says it mustn't. For validation that usually isn't what you want.

Anchors pin the ends down. ^ is the start of the string and $ is the end, so ^\d+$ matches a string that is digits and nothing else. Skip them and you're testing 'contains digits'. Keep them and you're testing 'is digits'.

\b is a word boundary — the seam between a word character and a non-word character. \bcat\b matches the animal, but not the cat inside 'category'.

Flags sit outside the pattern. The tester below offers three. g is global: find every match, not just the first. i makes matching case-insensitive. m is multiline: ^ and $ then match at line ends too. If the count looks wrong, check the flags before you rewrite anything.

Groups, choices and two worked patterns

Parentheses group pieces together, so a quantifier or choice can act on the whole group. They also capture — the engine remembers what each group matched, which is how you pull a value apart. Don't need the memory? Write (?:...) and the group won't capture.

The pipe is a plain or: cat|dog matches cat or dog. Group your choices when counts are involved — (cat|dog)+ isn't the same as cat|dog+, and the second one rarely does what its author hoped.

Now two patterns you'll actually reuse:

PatternWhat it checksReading it piece by piece
^[^\s@]+@[^\s@]+\.[^\s@]+$Basic email shapeStart; one or more non-space, non-@ characters; a literal @; more of the same; an escaped dot; more of the same; end. Shape only — it doesn't prove the mailbox exists.
(\d{4})-(\d{2})-(\d{2})A date like 2024-03-15Four digits captured, a dash, two digits captured, a dash, two digits captured. Run it over 'shipped 2024-03-15' and you get year, month and day as three groups.

Notice what the date pattern doesn't do. It won't blink at 2024-99-99. Shape, remember? If the value has to be a real calendar date, check that in code afterwards.

Build a pattern without guessing, step by step

  1. Write down strings that should match and strings that shouldn't. They're your test set, not an afterthought.
  2. Start with the literals — the fixed characters, like @ or dashes.
  3. Replace each changing run with the tightest class you can: \d for digits, [^\s@]+ for a chunk that can't contain a space.
  4. Add a quantifier to each run. Ask, 'how many, at least and at most?' and write that answer.
  5. Decide about position. If the whole string must fit, add ^ and $; if you're searching inside longer text, leave them off on purpose.
  6. Paste pattern and test strings into the tester below. The should-match strings should light up; the others should stay dark.

And if it fails? Don't rewrite the whole thing. Shorten it until the failure goes away, then add one piece back at a time. The piece that brings the failure back is the bug.

Test your pattern as you type

The Verystep Regex Tester runs JavaScript regular expressions against your test text with live highlighting, a match count, and the g, i and m flags described above. Paste in strings that should match and ones that shouldn't, and watch which light up. It runs in your browser; nothing leaves your device.

Open the Regex Tester ->

Last verified November 7, 2026 — character class, quantifier, anchor, grouping and g/i/m flag behaviour cross-checked against the JavaScript (ECMAScript) regex engine used by the tester below.

Frequently asked questions

Which regex flavour does the tester use?

JavaScript (ECMAScript), the flavour that runs in browsers and Node.js. The pieces here behave the same in most common flavours, but edge cases can differ, so test a pattern in the engine that will actually run it.

Why does \d+ match inside abc123?

Because nothing says where the match may sit, so the engine finds digits anywhere. Add anchors — ^\d+$ — and the digits only match when the whole string is digits.

Why does the tester say my pattern is invalid?

Usually an unescaped special character. Characters like ( [ and + have a special meaning, so a literal one needs a backslash. An unclosed bracket does the same thing. Shorten the pattern until it parses, then add pieces back.

Does the email pattern in this guide prove an address is valid?

No. It checks shape only — a non-space run, an @, another run, a dot, another run. It does not prove the domain exists or the mailbox is real. Treat it as a quick form check, not a verdict.

Is my test text uploaded anywhere?

No. The tester highlights matches entirely in your browser, so patterns and test strings stay on your device. That makes it safe for log lines you would not paste into a server-side tool.

More guides