Regex Exercises
Time to practise! Each exercise has a starter tester whose pattern is deliberately incomplete — it runs, but matches too much or too little. Edit the first line until the matches are exactly the ones described under "Expected", then compare with the solution.
Tip: Change one thing at a time and press Ctrl+Enter after each change. Watching how the match list reacts is the fastest way to learn.
| # | Exercise | Topics |
|---|---|---|
| 1 | Years 1900–2099 | quantifiers, alternation, word boundaries |
| 2 | Hex colours | classes, counted repetition |
| 3 | Hashtags | lookbehind |
| 4 | Usernames | anchors, multiline |
| 5 | Doubled words | backreferences |
| 6 | Prices | groups, optional parts |
| 7 | Lines without DEBUG | negative lookahead |
| 8 | Query-string pairs | named groups, negated classes |
1. Years 1900–2099
Match four-digit years from 1900 to 2099, but not parts of longer numbers or other numbers.
Expected: 1987, 2004, 2023 (3 matches).
/\d+/g
Born 1987, moved 2004, room 12, code 31415, year 2023, not 18506 matches
#1 "1987" at index 5
#2 "2004" at index 17
#3 "12" at index 28
#4 "31415" at index 37
#5 "2023" at index 49
#6 "1850" at index 59
Show solution
/\b(?:19|20)\d{2}\b/g(?:19|20) fixes the century, \d{2} adds two more digits, and the word boundaries stop 31415 from contributing a match.
2. Hex colours
Match CSS hex colours with exactly 3 or 6 hex digits.
Expected: #fff, #00AAff, #123 (3 matches).
/#\w+/g
#fff #00AAff #ggg #1234 #123 #abcdefg6 matches
#1 "#fff" at index 0
#2 "#00AAff" at index 5
#3 "#ggg" at index 13
#4 "#1234" at index 18
#5 "#123" at index 24
#6 "#abcdefg" at index 29
Show solution
/#(?:[0-9a-fA-F]{3}){1,2}\b/gThree hex digits, once or twice. The final \b rejects #1234 and #abcdefg, where more word characters follow.
3. Hashtags
Match hashtags (# followed by letters, digits or _), but not when the # is glued to a word, as in issue#42.
Expected: #regex, #JS_tips, #2025 (3 matches).
/#\w/g
Loving #regex and #JS_tips! See issue#42. #20254 matches
#1 "#r" at index 7
#2 "#J" at index 18
#3 "#4" at index 37
#4 "#2" at index 42
Show solution
/(?<!\w)#\w+/g\w+ takes the whole tag, and the negative lookbehind refuses a # that comes right after a word character.
4. Usernames
Each line is a username. A valid one is 3 to 16 characters long, uses only lowercase letters, digits and _, and starts with a letter. Match only the valid lines.
Expected: linh_99, bao, an_nguyen_2025 (3 matches).
/^[a-z]+/gm
linh_99
bao
x1
9lives
an_nguyen_2025
Mai
this_name_is_far_too_long5 matches
#1 "linh" at index 0
#2 "bao" at index 8
#3 "x" at index 12
#4 "an" at index 22
#5 "this" at index 41
Show solution
/^[a-z][a-z0-9_]{2,15}$/gmOne letter, then 2–15 more allowed characters (3–16 in total). Both anchors plus the m flag make each whole line pass or fail.
5. Doubled words
Find accidentally repeated words, ignoring case.
Expected: the the, is Is (2 matches), with the repeated word in group 1.
/\b(\w+)\s+\w+\b/gi
I saw the the cat. It is Is it not? Very good.5 matches
#1 "I saw" at index 0
group 1: "I"
#2 "the the" at index 6
group 1: "the"
#3 "It is" at index 19
group 1: "It"
#4 "Is it" at index 25
group 1: "Is"
#5 "Very good" at index 36
group 1: "Very"
Show solution
/\b(\w+)\s+\1\b/giThe backreference \1 requires the second word to repeat the first; the i flag makes the comparison case-insensitive.
6. Prices
Capture dollar amounts without the $ sign in group 1. Amounts may have thousands separators and an optional two-digit cents part.
Expected groups: 1,299.99, 15, 3.50 (3 matches).
/\$\d+/g
Total $1,299.99, shipping $15, tax $3.50, and 20 dollars.3 matches
#1 "$1" at index 6
#2 "$15" at index 26
#3 "$3" at index 35
Show solution
/\$(\d{1,3}(?:,\d{3})*(?:\.\d{2})?)/gOne to three digits, any number of ,ddd blocks, then optional cents — all inside group 1. Non-capturing groups keep the numbering simple.
7. Lines without DEBUG
Match every complete line that does not contain the word DEBUG.
Expected: the three INFO/WARN/ERROR lines.
/^.*$/gm
INFO server started
DEBUG config loaded
WARN disk 91% full
DEBUG cache warm
ERROR request failed5 matches
#1 "INFO server started" at index 0
#2 "DEBUG config loaded" at index 20
#3 "WARN disk 91% full" at index 40
#4 "DEBUG cache warm" at index 59
#5 "ERROR request failed" at index 76
Show solution
/^(?!.*\bDEBUG\b).*$/gmAt the start of each line, the negative lookahead scans ahead; if DEBUG appears anywhere in the line, the whole line is rejected.
8. Query-string pairs
Extract each key=value pair from a URL query string, with named groups key and value. Values may be empty.
Expected: page/2, sort/price_desc, q/red+shoes, debug/empty (4 matches).
/(\w+)=/g
/shop?page=2&sort=price_desc&q=red+shoes&debug=4 matches
#1 "page=" at index 6
group 1: "page"
#2 "sort=" at index 13
group 1: "sort"
#3 "q=" at index 29
group 1: "q"
#4 "debug=" at index 41
group 1: "debug"
Show solution
/[?&](?<key>\w+)=(?<value>[^&]*)/gEach pair starts after ? or &. The value is "anything up to the next &", and * allows it to be empty.
Test yourself
Which pattern matches a whole line only if it is 3–5 digits?
/\d{3,5}/gm/^\d{3,5}$/gm/^\d+$/gm/\b\d{3,5}/gm
In exercise 3, why use (?<!\w) instead of \b before #?
\bis not supported#is not a word character, so\b#requires a word character BEFORE it — the opposite of what we want- Lookbehind is faster
\bignores digits