Skip to chapter content
navan✳THE BUILD LABJAVASCRIPT
0/31 read
CHAPTER 20 / 31COLLECTIONS AND TEXT

Patterns over text.

A regular expression describes a match, not a complete understanding of text.

01 / THE BASICS

Start here.

A regular expression is a pattern for matching text. The literal /cat/ describes the consecutive letters c, a, t; /cat/.test('a cat naps') returns true. The slash marks the start and end of the pattern, not division in this context. This is a different question from string equality: the pattern can match part of a larger string.

Patterns have their own small language. A dot means any character except some line breaks; a star means zero or more of the preceding piece; a plus means one or more. Brackets such as [0-9] describe one character from a set or range. A caret at the beginning anchors a match to the start, and a dollar sign at the end anchors it to the end. Escaping with a backslash asks for a literal special character, such as \. for a period.

Begin with a small pattern and state what it accepts in plain words. For example, /^[0-9]+$/ means one or more digits from start to finish. It does not accept a minus sign, decimal point, or spaces. Regex is useful for finding and extracting patterns, but it does not understand every semantic rule of an email address, date, or whole programming language. When rules grow complex, simpler string operations or a dedicated parser can be clearer.

02 / THE IDEA

Build the model.

A regular expression is a compact description of text patterns. Character classes, repetition, anchors, and groups let you recognize or extract pieces of a string. The same compactness can make a pattern hard to review when it grows too clever.

Use regex for local, well-defined text shapes. Its rule should account for boundaries such as empty input, Unicode, and unexpected separators. Parsing a structured language or validating every possible email address usually calls for a parser or a purpose-built API instead of one enormous pattern.

03 / UNDER THE SURFACE

How it works.

Read a regular expression as a sequence of constraints. In /^[A-Z]{2}-\d{3}$/, ^ marks the beginning, [A-Z]{2} asks for exactly two uppercase ASCII letters, the hyphen is literal, \d{3} asks for three digits, and $ marks the end. The anchors matter: without them, a longer string could contain a valid-looking fragment and still pass test.

Keep a regex local to a precisely defined format. A pattern for a product code can be good; a single pattern claiming to understand every valid email address is fragile. When input may contain Unicode, decide whether ASCII ranges are intentional and whether a u flag is needed. A named pattern with clearly stated accepted and rejected shapes makes the rule understandable.

04 / A WORKED EXAMPLE

Read it line by line.

JAVASCRIPT / Worked example
const code = /^[A-Z]{2}-\d{3}$/;
code.test('AB-123'); // true

First notice. The anchors require the whole string to match the shape.

For 'AB-123', the engine starts at the beginning, consumes A and B for [A-Z]{2}, matches the literal hyphen, consumes 1, 2, and 3 for the three digits, and reaches the end as required by $. The test returns true. 'XAB-123Y' fails because the whole string does not fit.

05 / ANOTHER EXAMPLE

See the rule again.

JAVASCRIPT / Another example
const code = /^[A-Z]{2}-\d{3}$/;
console.log(code.test('NY-007'));
console.log(code.test('NY-7'));

The results are true and false. The first has two uppercase letters and exactly three digits. The second has only one digit after the hyphen. A pattern is a contract for a text shape; write examples that probe its boundaries.

06 / KEEP THIS

One thing to remember.

A readable pattern with clear limits is more useful than a magical one.

Finished this chapter?
Built with curiosity. By Navan Sami. ↗JavaScript · Understand the language behind the interface.