Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 38 ยท Getting Started

What Is a Regular Expression?

Learn what regular expressions are, what problems they solve, how a regex engine reads a pattern, and when to choose a regex over plain string methods. Includes runnable examples.

A regular expression (regex, regexp) is a small piece of text that describes a pattern. Instead of asking "does this string contain cat?", you can ask "does this string contain a three-letter word that starts with c and ends with t?" or "is this a valid date like 2026-11-12?". The program that reads the pattern and searches your text with it is called a regex engine.

Regexes are used in every serious language and in many tools: text editors, grep, databases, log analyzers, form validators and build scripts. Learn them once and you can use the same ideas almost everywhere. This course teaches the ideas with JavaScript (running on Node.js, right in your browser tab through our code runner), and later shows how other languages differ.

All examples in this track run for real on the site. Press Run in any editor, change the pattern, and watch what happens. Experimenting is the fastest way to learn regex.

The problem regexes solve

Plain string methods find exact text. That is enough when you know precisely what you are looking for.

Node.js Exact search with a string method

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

But what if you want every order number, whatever it is? You do not know the digits in advance. You know only the shape: a # followed by one or more digits. A regex describes shapes.

Node.js Describing a shape

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The pattern #\d+ reads: a literal #, then \d (a digit), + (one or more of them). By the end of this track you will read such patterns as easily as a sentence.

What a pattern is made of

Every regex is built from a few kinds of pieces. You will meet each one in its own lesson:

Four building blocks

Literals Letters that match themselves
Classes One char from a set
Quantifiers How many times
Groups Combine and capture

Everything else is a combination of these

1 / 3

How an engine reads your pattern

The engine works from left to right through the text. For each starting position it tries to match the pattern piece by piece. If a piece fails, it either backs up and tries another option or moves the start one character forward. The first starting position where the whole pattern succeeds gives you the match.

flowchart LR
  A["Start at position 0"] --> B{"Does the pattern match here?"}
  B -- "yes" --> C["Report the match"]
  B -- "no" --> D{"More positions?"}
  D -- "yes" --> E["Move start one character right"]
  E --> B
  D -- "no" --> F["No match"]

The engine tries every starting position until the pattern fits or the text ends.

You can watch this with exec, which reports where the match was found:

Node.js Where was it found?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The engine fails at positions 0, 1 and 2 (those are a), then succeeds at position 3 and keeps consuming b characters while it can.

Regex or string method?

Regexes are powerful, but not always the best tool. A good rule:

  • Use a string method (includes, startsWith, split(','), replaceAll('a', 'b')) when you search for fixed text. It is simpler, faster to read and harder to get wrong.
  • Use a regex when the text has a shape (digits, optional parts, alternatives, repeated groups) or when you need several matches or captured pieces at once.
  • Use a real parser for nested or recursive formats such as HTML, JSON, or programming languages. Regexes can handle small, well-defined slices of those, but a pattern that tries to parse all of HTML will fail on real pages.

If you can describe the thing as "a list of tokens separated by something" or "a line that looks like X", a regex is probably a good fit. If you need to count nesting depth, reach for a parser.

A first tour

Here are four tiny examples to carry into the next lessons. Do not worry about every symbol yet.

Node.js Four quick patterns

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a regular expression describe?

    A regex describes a pattern. Many different strings can match the same pattern.
  2. 2. When is a plain string method a better choice than a regex?

    For exact text, includes, startsWith or replaceAll are simpler and clearer than a pattern.
  3. 3. What does the engine do when the pattern fails at the current start position?

    The engine tries each starting position in turn. Only when none of them works is the result "no match".
  4. 4. What does /#\d+/g find in 'Order #1042 and #1043'?

    # is a literal, \d+ is one or more digits, and the g flag finds every match, so you get #1042 and #1043.

Summary

  • A regex is a pattern that describes the shape of text; an engine searches text with it.
  • Patterns combine literals, classes, quantifiers, groups, anchors, boundaries and lookaround.
  • The engine tries each start position from left to right and reports the first full match.
  • Use plain string methods for fixed text, regexes for shapes, and parsers for nested formats.

Next: Regex in JavaScript: The Basics