Find a course

Module 1: Regex FundamentalsLesson 1 of 1213 min

What Are Regular Expressions?

Learn what a regular expression is, how a regex engine scans text for matches, where regex is used, and how flags change matching behavior.

Step 1 of 13Learn

Ten thousand lines and one question

Your server log has 10,000 lines, and your manager wants every date on which an error occurred. Dates appear as 2026-03-14, buried in lines such as [2026-03-14 09:12:44] ERROR disk full. Searching for "2026-" finds the year but not the rest, and misses lines from 2025. Reading 10,000 lines by eye is not an option.

A regular expression (regex for short) is a small pattern language for describing text. This pattern describes "four digits, a dash, two digits, a dash, two digits":

\d{4}-\d{2}-\d{2}

Paste it into a text editor's search box with regex mode on, and every date lights up: 2025-11-30, 2026-03-14, all of them, with nothing else. Add a little more and you can match only the dates on ERROR lines, or rewrite every date into a different format in one step.

Regex is one of the most portable skills in computing. The same core syntax works in JavaScript, Python, Java, C#, Go, PHP and Ruby, in command-line tools such as grep and sed, in editors like VS Code, in spreadsheet functions, and in database queries. Learn it once, and you'll use it for the rest of your career.

Step 2 of 13Practice · Choose one

Which task is a good fit for a regular expression?

Step 3 of 13Learn

Literal characters and the engine

The simplest regex is plain text. The pattern cat matches the three characters c, a, t in that order, wherever they appear: in "cat," but also inside "concatenate" and "location." Most characters are literals that match themselves.

A regex engine is the part of a language or tool that runs the pattern. Conceptually, it works like this:

  1. Start at the first character of the text.
  2. Try to match the whole pattern beginning at that position.
  3. If it fails, move one character to the right and try again.
  4. Report the first position where the whole pattern matches (or every position, if asked to find all matches).

So cat against "the black cat sat" fails at t, h, e, the space, b, l, a, c (c matches but the next character is k, not a), and so on, until it succeeds starting at the c of "cat."

Two details trip up beginners. Matching is case-sensitive by default, so cat doesn't match "Cat." And a regex finds matches anywhere in the text unless you say otherwise, so a pattern meant to check that a whole string is a date will happily "match" the date inside a sentence. You'll fix both with flags and anchors.

Step 4 of 13Practice · Diagram

How the engine scans the text

Searching "the black cat" for cat: at the c of "black" the next letter is k. Which step follows?

Tap the shape that answers the question, then press Check.

How the engine scans the textCycle diagram: Stand at a position → Try the whole pattern here → Failed? Move one right → Matched? Report it.Leftmostmatch winsStand at apositionTry the wholepattern hereFailed? Moveone rightMatched?Report it
Stand at a position

It begins at the first character of the text.

Try the whole pattern here

Character by character, left to right through the pattern.

Failed? Move one right

Then try again from the new position.

Matched? Report it

With the g flag the engine continues after the match.

Step 5 of 13Practice · True or false

By default, the pattern dog finds a match in the text "hotdogs are popular".

Step 6 of 13Learn

Metacharacters: the special few

A small set of characters have special meanings in regex. They are called metacharacters:

.  ^  $  *  +  ?  (  )  [  ]  {  }  |  \

You'll learn each of them in this course. For example, the dot . means "any single character," so c.t matches "cat," "cot," "cut" and even "c7t."

When you need one of these characters literally, escape it with a backslash. To match the text "3.14", write 3\.14. Without the backslash, 3.14 would also match "3514" or "3-14," because the dot matches anything. To match a literal backslash, write two: \\.

Escaping mistakes are among the most common regex bugs, and they're sneaky because the unescaped pattern often still works on your test data. A pattern for prices like $9.99 has two problems: $ is a metacharacter (it means "end of text"), and the dot matches any character. The correct version is \$9\.99.

Outside a few contexts, escaping a character that doesn't need it is usually harmless, so when in doubt about punctuation, escaping is the safe choice.

Step 7 of 13Flashcards

The metacharacters, by job

Tap a card to flip it.

Step 8 of 13Practice · Spot the mistake

A developer wants to match the exact text "example.com/path?id=5" and wrote one pattern piece per token. Tap the two pieces that need a backslash escape to be matched literally.

Step 9 of 13Learn

Flags change the rules

Flags (also called modifiers) adjust how a whole pattern behaves. In JavaScript they go after the closing slash of a regex literal: /cat/gi. The most common ones:

FlagNameEffect
iignore case/cat/i matches "cat," "Cat" and "CAT"
gglobalfind every match, not just the first
mmultiline^ and $ match at the start and end of each line
sdotAll. also matches newline characters
uunicodetreat the pattern and text as full Unicode code points

Other languages spell flags differently. Python passes them as arguments, such as re.IGNORECASE, and many tools accept inline flags at the start of a pattern, like (?i)cat.

Flags matter more than they look. Without g, JavaScript's replace changes only the first match, which is a common source of "it only fixed one of them" bugs. Without i, a search for "error" misses "ERROR." Choosing flags is part of writing the pattern, not an afterthought.

Step 10 of 13Practice · Match the pairs

Match each JavaScript regex flag to its effect.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. i
  2. g
  3. m
  4. s

Step 11 of 13Practice · Code challenge

Write mentionsError(line) that returns true if the text contains the word "error" in any letter case (error, Error, ERROR...), and false otherwise. Use a regular expression with the right flag and the .test() method.

Runs in a sandbox in your browser.
  • mentionsError("2026-03-14 ERROR disk full")
  • mentionsError("Error: file not found")
  • mentionsError("all systems normal")
  • mentionsError("3 errors found")
  • mentionsError("")

Step 12 of 13Tap to reveal

Where regex shows up in everyday work

  1. Text editors and IDEs

    Find-and-replace with the regex option turns one-off cleanup into seconds: renaming getUser( to fetchUser( across a project, or turning a pasted list into code.

  2. Form validation

    Checking that a ZIP code, username or product code has the right shape before it reaches the server, and again on the server, because browsers can be bypassed.

  3. Log and data analysis

    Pulling dates, error codes, IP addresses or durations out of millions of log lines with grep or a short script, then counting or charting them.

  4. Data cleaning

    Normalizing phone numbers, trimming stray whitespace, splitting "Last, First" names or removing currency symbols before importing data into a spreadsheet or database.

  5. Routing and configuration

    Web servers, firewalls and build tools often accept regex to decide which URLs, files or requests a rule applies to, so a sloppy pattern can expose or block more than intended.

Step 13 of 13Practice · Choose one

Why does the pattern 3.5 match the text "345"?