Code of the Day
IntermediateGroups and references

Lab: groups and references

Practice capturing groups, named groups, non-capturing groups, and backreferences on realistic text extraction and transformation tasks.

Lab · optionalRegular ExpressionsIntermediate25 min
Recommended first
By the end of this lesson you will be able to:
  • Extract structured fields using capturing and named groups
  • Use backreferences to detect repeated content
  • Transform text format using captured groups in replacements

Optional lab. These exercises put the groups module to work on realistic tasks: parsing records, transforming dates, and finding suspicious repetition.

Warm up — see the difference

Capturing groups change a match from a yes/no answer into structured data. Try this and compare the two styles:

JavaScript — editable, runs in your browser

Checkpoint 1 — parse a CSV row

Extract the three fields from a simple CSV record. Fields may contain spaces; they are separated by commas.

Parse a CSV row into fieldsJavaScript

Write parseRow(row) that uses a regex with three capturing groups to extract [field1, field2, field3] from a comma-separated string. Return an array of the three trimmed string values.

parseRow('Alice,2024-03-15,admin')['Alice', '2024-03-15', 'admin']

Checkpoint 2 — reformat a date

Convert an ISO date (YYYY-MM-DD) to a human-readable format (March 15, 2024). The month names are provided as an array; use the captured month number as an index.

Reformat ISO date to human-readableJavaScript

Write humanDate(iso) converting "YYYY-MM-DD" to "Month D, YYYY" (e.g. "2024-03-05" becomes "March 5, 2024"). Use a regex with capturing groups to extract the parts.

humanDate('2024-03-15')'March 15, 2024'humanDate('2023-01-05')'January 5, 2023'

Checkpoint 3 — find duplicate words

Find all duplicate-word pairs in a sentence (consecutive occurrences of the same word, case-insensitive).

Find duplicate consecutive wordsJavaScript

Write findDuplicates(text) returning an array of duplicated words (lowercase) found in text. Use a backreference pattern.

findDuplicates('The the quick fox')['the']findDuplicates('it is is very very wrong')['is', 'very']

Checkpoint 4 — swap first and last name

Use backreferences in a replacement to transform "Last, First" into "First Last".

Swap last name, first name formatJavaScript

Write swapName(s) converting "LastName, FirstName" to "FirstName LastName". Use a capturing group replacement. First and last names may include hyphens.

swapName('Smith, John')'John Smith'swapName('Garcia-Lopez, Maria')'Maria Garcia-Lopez'

Done?

All four green? You can now turn a regex match into structured data and use captured groups to reshape text. These are the core tools of any text-processing pipeline.

Next: the Lookarounds module — (?=…), (?!…), (?<=…), and (?<!…) let you make assertions about what surrounds a match without consuming those surrounding characters.

Finished reading? Mark it complete to track your progress.

On this page