TExcelTAD
Home › Regular Expressions › Regex Intro

Regex Intro

A regular expression — "regex" or "regexp" for short — is a tiny language for describing the shape of text. Instead of searching for one fixed word, you describe a pattern such as "four digits, a dash, two digits" or "a word that starts with a capital letter", and the regex engine finds every piece of text that fits.

Almost every programming language, code editor, database and command-line tool understands regular expressions, so the time you invest here pays off everywhere.

What can you do with a regex?

Task Example
Search find every price like $12.99 in a document
Validate check that a postcode or product code has the right shape
Extract pull the date, level and message out of each log line
Replace turn 2025-04-12 into 12/04/2025 everywhere
Split break a line on commas or semicolons or tabs

How the playgrounds work

In this course most examples use the regex tester. The first line is a regex written the way JavaScript writes it — between slashes, followed by optional flags. Everything below the first line is the test text. Press ▶ Run and the tester highlights each match and lists it with its position (the index of its first character, counting from 0).

regex · try it yourself
/cat/g
The cat sat on the concatenated mat.
2 matches
#1 "cat" at index 4
#2 "cat" at index 22

The g after the closing slash is the global flag: "find all matches, not just the first". Notice that cat was also found inside concatenated — a regex does not know about words unless you tell it. You will learn how in the Word Boundaries lesson.

Try removing the g flag: only the first match is reported.

regex · try it yourself
/cat/
The cat sat on the concatenated mat.
1 match
#1 "cat" at index 4

A first taste of patterns

Regex becomes powerful once you use special characters. Here \d means "any digit" and + means "one or more of the previous thing", so \d+ finds every run of digits:

regex · try it yourself
/\d+/g
Room 12 has 3 desks and 140 books.
3 matches
#1 "12" at index 5
#2 "3" at index 12
#3 "140" at index 24

And parentheses capture parts of a match so a program can use them later. The tester lists each captured group under its match:

regex · try it yourself
/(\d{4})-(\d{2})-(\d{2})/g
Released 2024-11-05, patched 2025-02-17.
2 matches
#1 "2024-11-05" at index 9
   group 1: "2024"
   group 2: "11"
   group 3: "05"
#2 "2025-02-17" at index 29
   group 1: "2025"
   group 2: "02"
   group 3: "17"

Don't worry if \d{4} looks cryptic yet — each piece gets its own lesson.

Regex in a program

The same pattern works inside code. In JavaScript a regex literal is written between slashes:

js · try it yourself
const text = "Room 12 has 3 desks and 140 books.";
console.log(text.match(/\d+/g));
console.log(/desks/.test(text));
[ '12', '3', '140' ]
true

Note: Regex "flavours" differ slightly between languages. This course uses the JavaScript engine for the tester and points out differences in Python, Java, Go and others when they matter.

A word of advice

Regular expressions are compact, which makes them both powerful and easy to get wrong. Good habits:

  • Build patterns step by step and test each step against real examples.
  • Include text that should not match in your tests, not only text that should.
  • Prefer a clear pattern over a clever one — someone (maybe you) has to read it later.

Test yourself

In the tester, what does the first line contain?

  1. The test text
  2. The regex literal with optional flags
  3. The expected output
  4. A list of groups

Why did /cat/g match inside the word *concatenated*?

  1. Because of the g flag
  2. A regex searches for characters, not whole words, unless told otherwise
  3. It is a bug in the tester
  4. Because regex ignores case by default

What does the g flag do?

  1. Makes the match greedy
  2. Finds all matches instead of only the first
  3. Groups the pattern
  4. Ignores case