tools/regular-expressions-describe-the-shape-of-text-and-watch-a-pattern-fin.md

Regular expressions: describe the shape of text and watch a pattern find it

Tools · 4 min read ·

A regular expression describes the shape of text instead of spelling it out, so one short pattern can find every phone number or check that a field holds only digits.

This article builds up the pieces (character kinds like \d, quantifiers, anchors and groups), shows why <.+> grabs far more than you expect, and lets you edit a live matcher to see each match light up. It also says where regex is the wrong tool.

Dark card showing the pattern \d{3}-\d{4} highlighting two phone numbers in a sentence

You have a long list of text and you want every phone number in it. Or every line that starts with a date. Or you want to check that a form field contains only digits. Typing a rule for each case is slow. A regular expression (people say regex) lets you describe the shape of the text you want, and the computer finds everything with that shape.

A pattern is a description, not a word

Most characters in a pattern just match themselves: cat finds the letters c, a, t in a row. The power comes from a few special symbols that stand for "any one of a kind of character":

SymbolMatches
\done digit, 0 to 9
\wone letter, digit or underscore
\sone whitespace character (space, tab, newline)
.any one character except a newline
[abc]one of a, b or c

Because . is special, to match a real dot you write \.. The backslash means "treat the next symbol as plain text".

How many times? Quantifiers

A symbol on its own matches once. Put a quantifier after it to say how many repeats are allowed: + means one or more, * means zero or more, ? means zero or one, and {3} means exactly three. So \d{3}-\d{4} means three digits, a dash, four digits.

const s = "Call 555-0142 or 555-0199 before 5pm.";
console.log(s.match(/\d{3}-\d{4}/g));
// [ '555-0142', '555-0199' ]

That output comes from running the code in Node 22. The trailing g is a flag that means "find all matches, not just the first".

Try it yourself

Edit the pattern or the text. Matches light up as you type. Anything in round brackets becomes a numbered group listed underneath, which is how code pulls a piece out of a match.

Anchors and groups

Some symbols match a position instead of a character. ^ is the start of the text and $ is the end. To check that a whole field is digits, the pattern must stretch from start to end:

console.log(/^\d+$/.test("2026")); // true
console.log(/^\d+$/.test("20x6")); // false

Without the anchors, \d+ would happily match the "20" inside "20x6", and the check would pass. Pick the "Only digits?" button above, then delete the ^ and the $ to see it.

Round brackets save part of a match. Matching a date and keeping its pieces:

const m = "2026-11-14".match(/(\d{4})-(\d{2})-(\d{2})/);
console.log(m[0], m[1], m[2], m[3]);
// 2026-11-14 2026 11 14

m[0] is the whole match and m[1] onward are the groups, counted by the opening bracket from the left.

Greedy by default, and how to stop it

Quantifiers are greedy: they grab as much text as they can, then give some back only if the rest of the pattern needs it. Try the "Greedy" button. The pattern <.+> starts at the first <, lets .+ run to the very end of the text, then backs up until it finds a >. The result is one giant match covering both tags and the words between.

Adding a ? after the quantifier makes it lazy: it takes as little as possible. That is the "Lazy" button.

const h = "<b>hi</b> and <i>there</i>";
console.log(h.match(/<.+>/)[0]);   // <b>hi</b> and <i>there</i>
console.log(h.match(/<.+?>/g));  // [ '<b>', '</b>', '<i>', '</i>' ]

Where regex stops being the right tool

A regex is great for the shape of short, flat text: IDs, dates, search-and-replace in an editor, pulling fields out of log lines. It is a poor fit for nested structures. Real HTML, for example, has tags inside tags, and a pattern cannot track that nesting reliably, so use an HTML parser for it.

One more warning, because the demo above protects you from it. Patterns with a repeat inside a repeat, such as (a+)+b, can make some engines try an exponential number of ways to split the text before giving up. This is called catastrophic backtracking. The demo refuses patterns of that shape rather than freeze your browser tab. In your own code, keep patterns simple and avoid nested repeats on user-supplied text.

Handy habits: build a pattern one piece at a time, test each piece on real sample text, and escape the special symbols (. + * ? ( ) [ ] { } ^ $ \ |) with a backslash when you mean them literally. Syntax differs a little between languages, but everything on this page works in JavaScript, and most of it works almost unchanged in Python, Ruby and Go.

$ ls tools/

see all →
tools/file-permissions-what-rwxr-xr-x-means-and-why-a-script-says-permission.md File permissions: what rwxr-xr-x means, and why a script says Permission denied You run your first script and the terminal says Permission denied. Nothing is wrong with your code: every file and folder carries read… 6 min read · 2 Nov tools/git-stash-how-to-set-half-done-work-aside-without-committing-it.md Git stash: how to set half-done work aside without committing it You are halfway through a change when you need to switch branches, but the work isn't ready to commit. Git stash puts your unfinished edits… 4 min read · 30 Oct tools/gitignore-how-git-decides-which-files-to-skip-and-the-one-rule-that-su.md Gitignore: how Git decides which files to skip, and the one rule that surprises everyone Why does a file show up in git status when you wanted it hidden, and why does a file you ignored sometimes stay tracked anyway? Five short… 5 min read · 27 Oct tools/pipes-how-the-in-a-terminal-turns-small-commands-into-one-big-answer.md Pipes: how the | in a terminal turns small commands into one big answer How do you find the most popular page in a file of 15 million visits? In a terminal, you join small programs with the vertical bar `|`… 5 min read · 23 Oct tools/git-branches-why-making-one-doesn-t-copy-your-project.md Git branches: why making one doesn't copy your project When someone says "make a branch for that," it sounds like Git must copy your whole project. It doesn't. A branch is just a name stuck on… 4 min read · 20 Oct tools/git-staging-area-why-saving-your-work-takes-git-add-and-git-commit.md Git staging area: why saving your work takes git add and git commit Why does saving your work in Git take two commands, `git add` and `git commit`? Because a change travels through three places: your working… 4 min read · 17 Oct