Level 3: Advanced · Lesson 27 of 30
Validation 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.
Validation and extraction look similar, but they answer different questions. Validation asks: is this entire value acceptable? Extraction asks: where in this text are the values I care about? Mixing up the two is one of the most common sources of regex bugs.
A validation pattern must be anchored at both ends. To check a five-digit postal code, write ^\d{5}$. Without the anchors, \d{5} would happily find five digits inside 123456 and report success, so a form would accept a value that is clearly wrong. In JavaScript, test() searches by default, so the anchors are what turn a search into a whole-value check.
An extraction pattern must not be anchored to the whole input, because the values sit in the middle of other text. Instead, it uses word boundaries: \b\d{5}\b finds five-digit codes in a sentence while ignoring longer numbers such as 1234567. Extraction is usually run with the g flag so every occurrence is returned.
The m flag sits between the two. With it, ^ and $ match at the start and end of each line rather than the whole string. That lets you validate a list of values, one per line, in a single pass: ^\d{5}$ with m returns every valid line and skips the bad ones. Be careful not to turn it on accidentally in real validation, where a newline could let a bad value slip through.
Validation is also where strictness pays off. A validator should reject anything unexpected, so it uses exact character classes and counts, such as ^[a-z0-9_]{3,16}$ for a username. An extractor is often more forgiving because the surrounding text already gives context.
Remember the honest limits of both jobs. A regex checks shape, not truth. ^\d{5}$ cannot tell whether a postal code exists, and an email pattern cannot tell whether the mailbox is real. Use regex as a first filter, then confirm with real data or a confirmation step where it matters.
When you take a library pattern and use it in your own code, check which kind it is. Add anchors to use it for validation, or swap anchors for word boundaries to use it for extraction.
Live tester
2 matches (JavaScript engine)
12345 1234 54321 123456
Examples
Numbers
/\b\d{4}\b/Years 1999, 2026 and 12345 were logged.
Matches: “1999”, “2026”
Extraction with word boundaries finds four-digit years and skips the five-digit number.
Text
/^[A-Za-z]+$/mhello hello world World
Matches: “hello”, “World”
Line-by-line validation accepts single words and rejects the line that contains a space.
Codes
/^[A-Z]{2}\d{6}$/mAB123456 AB12345 XY000001
Matches: “AB123456”, “XY000001”
Each line must be exactly two letters and six digits to pass.
Practice tasks
Your regex runs over the text with the global flag. It passes when it finds every required match and nothing that should be left out.
Task 1: Validate one postal code per line: exactly five digits and nothing else.
12345 123456 54321
Task 2: Extract every standalone five-digit code from the sentence.
Ship to 75500 or 12345, not 123 or 1234567
Task 3: Validate usernames, one per line: 3 to 16 lowercase letters, digits or underscores.
ali_test ab sara99 bad name