/^/

Lesson 1 of 28

What is Regex

A gentle introduction to regular expressions — what they are, why they exist, and where you'll use them every day.

5 min read

A regular expression (regex, for short) is a sequence of characters that defines a search pattern. Instead of searching for one exact string, a regex describes a whole family of strings — “any digit repeated three times,” “anything that looks like an email address,” “a word that starts with a capital letter.”

Regex isn’t a programming language on its own — it’s a small, standalone syntax that almost every programming language, text editor, and command-line tool understands: JavaScript, Python, Java, grep, VS Code’s search bar, SQL, and dozens more.

Why regex exists

Before regex, matching text patterns meant writing custom string-parsing code by hand — loops, character-by-character comparisons, endless edge cases. Regex compresses all of that into a compact notation that a regex engine can execute directly.

Compare these two ways of checking “does this string look like a US-style phone number?”:

// Without regex — verbose and easy to get wrong
function looksLikePhone(s) {
  if (s.length !== 12) return false;
  for (let i = 0; i < s.length; i++) {
    const c = s[i];
    if (i === 3 || i === 7) {
      if (c !== "-") return false;
    } else if (c < "0" || c > "9") {
      return false;
    }
  }
  return true;
}
// With regex — one line, and easier to read once you know the syntax
const looksLikePhone = (s) => /^\d{3}-\d{3}-\d{4}$/.test(s);
555-123-4567 5551234567 555-123-45678

Where you’ll actually use regex

  • Form validation — checking that an email, phone number, or password meets a format before submitting
  • Search and replace — renaming variables across a codebase, cleaning up messy data
  • Parsing logs — extracting timestamps, error codes, or IP addresses from log files
  • Data extraction — pulling structured data (dates, prices, URLs) out of unstructured text
  • Input sanitization — stripping unwanted characters or whitespace

Tip

Regex is best for shallow, pattern-based matching — things with a flat, repeatable shape. It struggles with deeply nested or context-sensitive structures, which is why “don’t parse HTML with regex” is a common piece of advice you’ll hear later in this course.

The building blocks

Every regex is built from a small set of ingredients that you’ll learn one at a time in this course:

Concept Example Meaning
Literal characters cat Matches the exact text “cat”
Character classes [aeiou] Matches any one vowel
Quantifiers a{2,4} Matches 2 to 4 a’s in a row
Anchors ^Hello Matches “Hello” only at the start of a string
Groups (ab)+ Matches “ab” repeated one or more times

Don’t worry if none of that fully makes sense yet — each of these gets its own lesson, with interactive demos you can experiment with directly in the browser.

Try it yourself

Open the demo above and change the test text. Try inputs like "call 555.123.4567" or "555-1234" — notice how the pattern only matches the exact \d{3}-\d{3}-\d{4} shape, nothing looser.

What should happen

"call 555.123.4567" won’t match at all, because the pattern requires hyphens (-), not dots, between the digit groups. "555-1234" won’t match either, because it’s missing the first three digits. The pattern ^\d{3}-\d{3}-\d{4}$ is strict on purpose — you’ll learn how to loosen or tighten patterns like this throughout the course.

What’s next

In the next lesson, you’ll learn about regex engines — the actual programs that take a pattern and a piece of text and figure out whether (and where) they match.

Quick check

What does 'regex' stand for?

Which of these is NOT a typical use case for regex?