RegexLab

Regex Basics: How Pattern Matching Works

6 min readUpdated September 2026
Lines of source code on a screen

A regular expression — regex for short — is a compact way to describe a pattern of text. Instead of writing code that loops over characters, you write a pattern like \d{4}-\d{2}-\d{2} and let the regex engine do the searching. That single skill powers find-and-replace in editors, form validation, log parsing and data cleaning. This guide covers the mental model you need before anything else clicks.

Literals and metacharacters

Most characters in a pattern are literals: the regex cat matches the three letters c, a, t in that order, anywhere in the text. The power comes from metacharacters — symbols with special meaning. The dot . matches any single character; \d matches a digit; \w matches a word character (letters, digits and underscore); and \s matches whitespace. Their uppercase versions \D, \W and \S mean the opposite. So \w+\s\d+ describes a word, then a space, then a number.

Because metacharacters are special, matching one literally means escaping it with a backslash. To match a real dot you write \., and to match a backslash you write \\. Forgetting to escape is the single most common beginner mistake — the pattern price: $5 will not find the dollar sign unless you write price: \$5.

How the engine scans

The engine reads the text left to right. At each position it tries to match the pattern; if it fails, it moves one character forward and tries again. This is why a/ b\d might match at position 7 even though your string is long — the engine simply keeps sliding until something fits or the text runs out. Understanding this sliding-window behaviour explains why anchors matter (see below) and why a greedy pattern can grab more than you expect.

Anchors pin a match to a position rather than a character. The caret ^ matches the start of the string (or line, with the m flag), and the dollar $ matches the end. So ^\d+$ means the entire string must be nothing but digits — a common way to validate that a field contains only a number.

Alternation and quantifiers at a glance

Two more building blocks appear immediately. The pipe | means "or": cat|dog matches either word. Quantifiers repeat the preceding item: * means zero or more, + means one or more, ? means optional (zero or one), and {2,4} means between two and four times. Combine them and colou?r matches both the British and American spellings, while \d{3}-\d{4} matches a seven-digit phone number.

The fastest way to internalise all of this is to try it. Paste a few sample lines into the tester, type a pattern, and watch matches light up as you add each piece. Toggling a flag or adding a quantifier and seeing the highlight change teaches more than any table of symbols.

Anchors and word boundaries

Metacharacters that match a position rather than a character are called anchors, and they are what stop a pattern drifting. `^` matches the start of the string, `$` the end, so `^\d{4}$` accepts `2026` but rejects `in 2026`. Without anchors a pattern is a search; with them it becomes a test of the whole string, and that distinction is behind a large share of validation bugs.

`\b` is subtler: it matches the empty position between a word character and a non-word character, where "word character" means exactly `[A-Za-z0-9_]`. So `\bcat\b` will not match inside `concatenate`, which is usually what you want — but it also treats an accented letter as a non-word character, so `\bcafé\b` breaks at the `é` in JavaScript unless you switch to Unicode property escapes. `\B` is its negation and is mostly useful for finding a substring *inside* a word.

Escaping, and testing a pattern honestly

Twelve characters carry special meaning outside a character class — `. * + ? ( ) [ ] { } ^ $ | \` — and a backslash turns any of them back into a literal. Inside square brackets most of them relax: `[.+*]` matches those three characters literally, and only `^` (first), `]`, `-` and `\` still need care. When a pattern comes from user input, build it with a helper that escapes every special character rather than concatenating raw text.

The habit that separates a working pattern from a lucky one is testing the negatives. It is easy to confirm that `^\d+$` matches `42`; the useful question is whether it rejects an empty string, a leading `+`, a decimal point and a string with a trailing newline — and `$` in JavaScript does allow a final newline in multiline mode. Collect a handful of strings that must fail and keep them next to the ones that must pass.

The takeaway

Regex is literals plus metacharacters, scanned left to right. Learn the dot, \d, \w, \s, anchors, alternation and the four quantifiers, and you can read most patterns. Open the tester and build one piece at a time.

Try it live
Open the regex tester

Try this pattern live with match highlighting.