TutreNews Learn

Regular Expressions: Pattern Matching for Text

Learn to describe text patterns precisely: literals, character classes, quantifiers, anchors, groups, lazy matching, lookahead and the JavaScript methods that use them. Build and test patterns for dates, e-mail addresses, log lines and more.

Beginner · 3 units · 12 lessons · 87 exercises · ~1.8 h

What you will learn

  • Read and write patterns with literals, classes and quantifiers
  • Anchor patterns to the start, end and word boundaries of text
  • Group, capture and reuse parts of a match
  • Explain greedy versus lazy matching and choose correctly
  • Use test, match, matchAll, replace and split in JavaScript
  • Recognise risky patterns such as catastrophic backtracking

Prerequisites

  • Basic JavaScript: strings, variables and console.log

Learning path

1 Matching characters

  1. Literals, the dot and escaping8 min · 6 exercises
  2. Character classes9 min · 5 exercises
  3. Quantifiers9 min · 5 exercises
  4. Anchors and word boundaries8 min · 5 exercises
  5. Unit exam6 exercises

2 Groups, choices and lazy matching

  1. Alternation and groups8 min · 5 exercises
  2. Capturing groups and backreferences10 min · 5 exercises
  3. Greedy and lazy matching9 min · 4 exercises
  4. Flags7 min · 4 exercises
  5. Unit exam6 exercises

3 Regex in practice

  1. Regex methods in JavaScript10 min · 5 exercises
  2. Lookahead9 min · 5 exercises
  3. Real-world patterns10 min · 5 exercises
  4. Pitfalls, performance and limits9 min · 5 exercises
  5. Unit exam5 exercises
  1. Final exam11 exercises

Practical project: Build a text-cleaning and log-analysis toolkit

  1. Write a function normalizeSpaces(s) that replaces every run of whitespace by one space and trims the ends, using only replace with regular expressions.
  2. Write extractDates(text) that returns all dates in the form dd.mm.yyyy found in a text, and toIso(date) that converts one of them to yyyy-mm-dd with capture groups.
  3. Write parseLog(line) that returns an object { date, time, level, message } for lines like "2026-12-05 14:03:22 ERROR disk full" (or null for lines that do not fit). Use named groups.
  4. Take 20 log lines (invent them, including 4 malformed ones) and print how many lines exist per level.
  5. Write escapeRegex(s) and a function highlight(text, term) that wraps every occurrence of a user-supplied term in square brackets, case-insensitively.
  6. Write a short list of 10 test strings per function, including cases that must fail, and print a pass/fail summary.

References

Related courses