Skip to content

extract

Module Export

text |> (pattern: regex) => [{ value: text, groups: { text|number -> text } }]

Extracts regex matches and capture groups from a text.

extract finds all matches of a regex pattern in the input text and returns an iterator of records. Each record contains:

  • value: the full matched text
  • groups: a map of captured groups, where named groups use text keys and unnamed groups use 1-based numeric keys

Examples

Extract matches without groups

import { extract } from 'text'
from 'abc123def456' extract /\d+/
// Returns:
[
{ value = '123', groups = {} },
{ value = '456', groups = {} }
]

Extract with unnamed groups

import { extract } from 'text'
from 'abc123def456' extract /([a-z]+)(\d+)/
// Returns:
[
{ value = 'abc123', groups = { 1 -> 'abc', 2 -> '123' } },
{ value = 'def456', groups = { 1 -> 'def', 2 -> '456' } }
]

Extract with named groups

import { extract } from 'text'
from '2026-02-27' extract /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/
// Returns:
[
{ value = '2026-02-27', groups = { 'year' -> '2026', 'month' -> '02', 'day' -> '27' } }
]

Extract with mixed named and unnamed groups

import { extract } from 'text'
from 'hello 42' extract /(?<word>[a-z]+) (\d+)/
// Returns:
[
{ value = 'hello 42', groups = { 'word' -> 'hello', 1 -> '42' } }
]

No matches returns an empty iterator

import { extract } from 'text'
from 'hello world' extract /\d+/
// Returns:
[]

Notes

  • Named groups are keyed by their name (text), unnamed groups are keyed by their 1-based index (number)