Free interactive course
Learn Regex in 30 Lessons
Short, practical lessons. Each one explains a single idea, gives you a live tester, three examples (numbers, text and codes), three practice tasks that check your answer automatically, and a three-question quiz. Your progress is saved in this browser.
Completed: 0 of 30
Level 1: Foundations
- 1What is Regex A regular expression is a small pattern that describes text you want to find. Learn what a regex is, where it runs, and how a regex engine reads text from left to right.
- 2Literal text Literal characters match themselves, exactly and in order. This lesson shows how literal patterns behave with spaces, digits and partial words.
- 3The dot . The dot matches any single character except a line break. It is the first wildcard you learn and the easiest one to overuse.
- 4Quantifiers * + ? Quantifiers say how many times the previous item may repeat. Learn the three basic ones: * for zero or more, + for one or more and ? for optional.
- 5Character sets [ ] Square brackets define a set of allowed characters for one position. Use them to match one of several letters, a range like a-z, or a mix of both.
- 6Shorthands \d \w \s Shorthand classes are short names for common character sets: \d for digits, \w for word characters and \s for whitespace, plus their uppercase opposites.
- 7Anchors ^ $ Anchors match a position, not a character. ^ pins a match to the start and $ to the end of the text, or of each line in multiline mode.
- 8Word boundary \b The \b anchor matches the edge of a word. It lets you find whole words like cat without matching the letters inside concat or cats.
- 9Groups ( ) Parentheses group several characters into one unit so you can repeat them or make them optional, and they capture the matched text for later use.
- 10Mini project: extract codes Put the first nine lessons together to extract order codes like ORD-1001 from messy text, while skipping lookalikes and broken codes.
Level 2: Practical patterns
- 11Counts {n,m} Curly braces let you say exactly how many times something repeats: {3} for exactly three, {2,4} for two to four, and {2,} for two or more.
- 12Alternation | The pipe | means or. It lets a pattern match one of several alternatives, and groups control how far each alternative reaches.
- 13Negated sets [^ ] A caret at the start of a character set flips it: [^,] matches any single character except a comma. Negated sets are perfect for reading up to a delimiter.
- 14Escaping special characters A backslash turns a metacharacter into a literal. Learn which characters need escaping, and how string escaping in your language adds a second layer.
- 15Flags (g i m s u y) Flags change how a whole pattern behaves: global search, case-insensitive matching, multiline anchors, dot-matches-newline, Unicode mode and sticky matching.
- 16Non-Capturing Groups Use (?:...) to group tokens for repetition or alternation without creating a numbered capture. Your patterns stay tidy and your group numbers stay predictable.
- 17Lookahead Lookahead checks what comes next without consuming it. Use (?=...) to require something after a match and (?!...) to forbid it.
- 18Lookbehind Lookbehind checks what comes before a match without including it. Use (?<=...) to require a prefix and (?<!...) to forbid one.
- 19Greedy vs Lazy Quantifiers Quantifiers are greedy by default and grab as much as they can. Adding ? makes them lazy so they stop at the first point where the rest of the pattern can match.
- 20Replace with $1 Capture groups let a replacement reuse pieces of the match. Write $1, $2 and friends in the replacement string to rearrange text.
Level 3: Advanced
- 21Named Groups Named groups such as (?<year>\d{4}) label captures with meaningful names. Your code reads groups.year instead of guessing what group 3 was.
- 22Backreferences A backreference like \1 matches the exact text an earlier group captured. Use it to find doubled words, matching quotes and paired tags.
- 23ReDoS and Catastrophic Backtracking Some patterns take exponential time on certain inputs. Learn to spot nested quantifiers and overlapping alternatives, and how RegexLab protects you with a timeout.
- 24Unicode and Non-Latin Text \w and [a-z] only cover basic Latin letters in JavaScript. With the u flag, Unicode properties such as \p{L} and \p{Script=Arabic} match letters from any language.
- 25Generic Extraction Strategies Pull values out of messy text by anchoring on labels and delimiters. Learn a reusable toolkit of negated classes, lookarounds and captures for extraction jobs.
- 26Build Your Own Pattern A repeatable method for writing a new pattern from scratch: collect samples, start literal, generalise piece by piece, add boundaries, and test against near misses.
- 27Validation vs Extraction Validation asks whether the whole input has the right shape; extraction finds matching pieces inside larger text. The same format needs different anchors for each job.
- 28Why Regex Differs Between Languages Every language ships its own regex engine with its own features and syntax. Learn the differences that most often break a pattern when you move it between JavaScript, Python, Go and others.
- 29Debugging a Pattern When a pattern misbehaves, shrink it, test it piece by piece, and check the usual suspects: unescaped characters, greedy wildcards, missing anchors and flags.
- 30Capstone: Parse a Log File Bring everything together by pulling dates, levels, messages, emails and addresses out of a realistic log. Each task combines several techniques from earlier lessons.
How to use these lessons
Work through Level 1 in order; each lesson builds on the previous one. Level 2 covers the tools you will use every day, such as counted repetition, alternation, lookarounds and replacements. Level 3 is about writing patterns that are correct, fast and portable across languages, including named groups, backreferences, catastrophic backtracking and Unicode text.
Keep the CheatSheet open in another tab, and use the Playground to experiment with your own data.