You’ve now met every major building block of regex: character classes, anchors, quantifiers, groups, and lookaround. The five exercises below don’t teach anything new — they’re here so you can practice pulling those pieces together, in patterns that get progressively harder. Try to write each pattern yourself before opening the solution.
Exercise 1 — Character classes: match a single alphanumeric character
Write a pattern that matches exactly one character that is either a letter (upper or lowercase) or a digit — nothing else.
Show solution
/[A-Za-z0-9]/g[A-Za-z0-9] combines three ranges inside one character class: lowercase letters, uppercase letters, and digits. Against "a1! B2 #", it matches a, 1, B, 2 individually, and correctly skips the space, !, and #.
Exercise 2 — Anchors: match markdown-style headings
Write a pattern that matches a # character, but only when it appears at the very start of a line (like a markdown heading), not a # that shows up mid-line.
Show solution
/^#/gmThe ^ anchor means “start of a line” here rather than “start of the whole string” — but only because of the m (multiline) flag. Without m, ^ would only match at the very beginning of the entire input, and a heading on line 3 would never match. Against a block like:
# Title
Some text
## Subtitle
not a heading #this matches the # at the start of # Title and the first # of ## Subtitle, but correctly ignores the trailing # in the last line, since that one isn’t at the start of its line.
Exercise 3 — Groups: detect an accidentally repeated word
Write a pattern that finds a word immediately repeated, like the typo “the the” or “is is” — case-insensitively — using a capturing group and a backreference.
Show solution
/\b(\w+)\s+\1\b/gi(\w+) captures a word into group 1, \s+ requires whitespace after it, and \1 demands that the exact same text appears again immediately after. Against the demo text, it finds "the the", "is is", and "here here" — but correctly skips "cat sat", since “cat” and “sat” aren’t the same word. The i flag makes the backreference comparison case-insensitive too, so “The the” would still count as a repeat.
Exercise 4 — Lookaround: extract pixel values without the unit
Given a CSS-like string with several measurements, write a pattern that captures just the numbers that are immediately followed by px — without including px in the match itself.
Show solution
/\d+(?=px)/g(?=px) is a positive lookahead — it checks that px comes next without consuming those characters as part of the match. So \d+(?=px) matches 300 and 10 (both followed by px), but skips 45, since it’s followed by em instead. This is the same trick you’d use to strip units, currency symbols, or any suffix you need to check for but don’t want in the extracted value.
Exercise 5 — Combining everything: a simple password rule
Write a pattern that matches a line only if it is at least 8 characters long, contains at least one uppercase letter, and contains at least one digit — using lookahead, since a single quantifier can’t check three independent conditions on the same text.
Show solution
/^(?=.*[A-Z])(?=.*\d).{8,}$/gmThis pattern uses two lookaheads stacked at the very start: (?=.*[A-Z]) checks “somewhere ahead, there’s an uppercase letter” and (?=.*\d) checks “somewhere ahead, there’s a digit” — neither lookahead consumes any characters, so both get to check the entire rest of the line independently. Only after both conditions are confirmed does .{8,}$ actually consume the line, requiring at least 8 characters total.
Against the five test lines: password fails (no uppercase, no digit), Password1 and PASS1234 and P4ssword123 all pass, and longenoughbutnodigitsoruppercase fails despite being plenty long, because it has neither an uppercase letter nor a digit. This is exactly the pattern shape you’ll see in real sign-up form validation.
What’s next
If these felt comfortable, the graded Challenges section from the last lesson is the natural next step — harder problems, less hand-holding, real solutions to check your work against. Either way, the final lesson of this course wraps everything up with a short recap and a full course quiz.