Lesson 1 of 6

Why Regular Expressions?

Say you need to check whether a string "looks like" an email address, or pull every phone number out of a wall of pasted text, or find every place in a codebase where a variable is used but not just any place that contains its name as a substring. Plain string methods like .indexOf() or .includes() can only ask "does this exact text appear?" They have no way to ask "does text matching this shape appear?" That's the gap regular expressions fill.

💡 The Bottom Line

A regular expression (regex) is a tiny, specialized language for describing a pattern of text instead of a literal, exact piece of text. Once you can describe "a digit, twice, followed by a dash" instead of just matching a fixed string, you can validate, search, and extract things that plain string methods simply can't express.

What plain string methods can't do

Suppose you want to check "is this a valid-looking US zip code?" A zip code is five digits, optionally followed by a dash and four more digits. You could write a pile of loops checking character-by-character whether each one is a digit, handling the optional dash-and-four-digits suffix as a special case, and so on. Or you could write one line of regex:

^\d{5}(-\d{4})?$

That single line says: start of string, exactly five digits, optionally a dash and four more digits, end of string. You'll unpack exactly what each symbol means over the next few lessons — for now, notice that this is describing a shape, not a specific zip code.

A little history

Regular expressions didn't start in a web browser — they come from 1950s mathematical work on describing patterns in formal languages, and became genuinely useful once Unix tools picked them up in the 1970s. Classic command-line tools like grep ("global regular expression print"), sed (stream editor), and awk all built their entire search-and transform workflows around regex. That heritage is why regex syntax feels a little terse and cryptic today — it was designed for fast, dense one-liners typed at a terminal, not for readability at a glance.

🙋 There Are No Dumb Questions

Q: Is regex the same in every programming language?
A: Mostly, but not perfectly. The core syntax — character classes, quantifiers, groups — is shared across almost every language's regex engine (this is often called "PCRE-style," after Perl Compatible Regular Expressions). But there are small differences: JavaScript needs a flag for multi-line lookbehind support in older engines, Python's re module has slightly different named-group syntax, and so on. What you learn here will transfer almost everywhere, with minor syntax adjustments.

Where you'll actually use it

📝 Form validation

Checking that an email, phone number, or password meets a required shape before submitting a form.

🔎 Search-and-replace

Your code editor's "Find & Replace" almost certainly has a regex mode for pattern-based edits across many files at once.

📄 Log parsing

Pulling timestamps, error codes, or IP addresses out of raw server log lines.

📊 Data extraction

Scraping structured pieces — prices, dates, hashtags — out of unstructured or semi-structured text.

⚠️ Watch Out

Regex is powerful, but it's not always the right tool. If you just need to check "does this string contain this exact word," a plain string method is faster to write, faster to run, and far easier for the next person to read. Reach for regex when you need to describe a pattern, not just a literal value.

🔧 Try It Yourself

Open your browser's developer console and try /^\d{5}$/.test("90210") — you should get true. Then try it on "9021" or "9021x" and see it return false. You've just run your first regex, before knowing any of the syntax in detail — that's the point of the next few lessons.

✅ Quick Check

What can a regular expression do that a plain string method like .includes() can't?


Next up: the basic building blocks — literal characters, the dot wildcard, and anchors that pin a match to the start or end of a string.

← Back to
Next →