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.
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
2 Groups, choices and lazy matching
3 Regex in practice
Practical project: Build a text-cleaning and log-analysis toolkit
- Write a function
normalizeSpaces(s)that replaces every run of whitespace by one space and trims the ends, using onlyreplacewith regular expressions. - Write
extractDates(text)that returns all dates in the form dd.mm.yyyy found in a text, andtoIso(date)that converts one of them to yyyy-mm-dd with capture groups. - Write
parseLog(line)that returns an object{ date, time, level, message }for lines like "2026-12-05 14:03:22 ERROR disk full" (ornullfor lines that do not fit). Use named groups. - Take 20 log lines (invent them, including 4 malformed ones) and print how many lines exist per level.
- Write
escapeRegex(s)and a functionhighlight(text, term)that wraps every occurrence of a user-supplied term in square brackets, case-insensitively. - Write a short list of 10 test strings per function, including cases that must fail, and print a pass/fail summary.
References
Related courses
JavaScript Fundamentals
The language of the web, from the ground up: values, types and operators, control flow, functions and arrow functions, arrays with…
Python Fundamentals
Learn to program with Python from scratch: values and variables, decisions, loops, lists, dictionaries, strings, functions and error handling. Every…
SQL Fundamentals: Querying Relational Databases
Learn SQL with a realistic shop database: SELECT and WHERE, sorting, aggregate functions, GROUP BY and HAVING, joins, subqueries, and changing data…