Code of the Day
BeginnerCore syntax

Lab: core syntax

Apply literals, character classes, quantifiers, and anchors to practical matching challenges.

Lab · optionalRegular ExpressionsBeginner20 min
Recommended first
By the end of this lesson you will be able to:
  • Write patterns that validate real-world formats using anchors and character classes
  • Apply quantifiers to match variable-length input
  • Combine multiple concepts into a single working pattern

Optional lab. These challenges apply everything from the Core syntax module. Work through them in order — each one builds on the last. Run the code in your browser and edit freely; there are no wrong experiments.

Warm up — feel the engine

Before the graded exercises, try the pattern editor below. Change /\d+/g to different patterns and observe what highlights:

JavaScript — editable, runs in your browser

Checkpoint 1 — US ZIP code

A US ZIP code is exactly five digits, optionally followed by a hyphen and four more digits (12345 or 12345-6789).

Match a US ZIP codeJavaScript

Write isZip(s) returning true if s is a valid US ZIP code (5 digits, or 5 digits + hyphen + 4 digits) and false otherwise. The entire string must be a ZIP code — no extra characters.

isZip('12345')trueisZip('12345-6789')trueisZip('1234')false

Checkpoint 2 — ISO date

An ISO date has the form YYYY-MM-DD — a four-digit year, a two-digit month, and a two-digit day, separated by hyphens.

Match an ISO dateJavaScript

Write isIsoDate(s) returning true if s matches the format YYYY-MM-DD (four digits, hyphen, two digits, hyphen, two digits). The pattern should match the entire string.

isIsoDate('2024-03-15')trueisIsoDate('2024/03/15')false

Checkpoint 3 — find all numbers in text

Extract every integer or decimal number from a string. A number here is one or more digits, optionally followed by a dot and more digits.

Find all numbers in a stringJavaScript

Write findNumbers(s) returning an array of all integer or decimal numbers found in s. A number is one or more digits, optionally followed by a decimal point and more digits.

findNumbers('Order 3 items at 5 each')['3', '5']findNumbers('Price: 9.99, tax: 0.75')['9.99', '0.75']

Find all occurrences of a word in a string without matching it as part of a larger word. For example, "set" should not match in "settings" or "reset".

Find whole-word matchesJavaScript

Write findWord(text, word) returning an array of all whole-word occurrences of word in text (case-insensitive). Use word boundaries to avoid partial matches.

findWord('set the settings', 'set')['set']findWord('Set the SET', 'set')['Set', 'SET']

The starter code uses new RegExp(...) instead of a literal /pattern/ because the word to search for is a variable. This is the standard way to build a pattern dynamically at runtime.

Checkpoint 5 — line-start filter

Given a multi-line string, return all lines that begin with a # character (markdown headings, shell comments, etc.).

Find lines beginning with #JavaScript

Write getHeadings(text) returning an array of all lines in text that begin with the # character. Use the multiline flag so ^ matches line starts.

getHeadings('# Title\nsome text\n## Section')['# Title', '## Section']

Done?

All five green? You have applied the full core syntax toolkit to realistic problems. Next up: the Practical matching module — groups, alternation, flags in depth, and a library of common real-world patterns.

Finished reading? Mark it complete to track your progress.

On this page