Language reference · section 27
27. Regex blocks
A regex block names a pattern, lists its parts, and checks it with examples.
Feature: Regex blocks
Scenario: regex matches the whole value
Given a regex block
When the value is tested
Then the whole value must match the pattern
Scenario: find matches anywhere in the text
Given a find block
When the text is tested
Then the pattern may match any part of the text
Scenario: A regex block needs a row that is matched
Given a regex block with only part rows
Then the compiler reports an error "`p` needs at least one row that is matched, not only `part` rows"
Scenario: Each row is a pattern and a name
Given the row "\d+ amount"
Then the last word is the name and the rest is the pattern
Scenario: A row needs a pattern and then a name
Given the row "\d+"
Then the compiler reports an error "a regex row needs a pattern and then a name, like `\d+ digits`"
Scenario: part defines a named piece without matching it
Given a part row before a matching row
Then a later row can insert the part with <name>
Scenario: An undefined part name is an error
Given a row that uses <name> without a part above it
Then the compiler reports an error "`<name>` is not a part defined above it"
Scenario: matches and rejects examples are checked at build time
Given a matches example that the pattern rejects
Then the compiler reports an error "matches \"x\", but the pattern rejects it"
Scenario: Flags are i, m, s, and u, each at most once
Given the block "regex i:"
Then the flag i applies to the generated RegExp
Scenario: An unknown flag is an error
Given the block "regex q:"
Then the compiler reports an error "unknown flag `q`; a regex block takes i, m, s, or u"
Scenario: A flag can appear only once
Given the block "regex i i:"
Then the compiler reports an error "the flag `i` is given twice"
Scenario: A repeat that can match its own separator in many ways is an error
Given a row with "(a+)+"
Then the compiler reports an error because the pattern can take exponential time
Scenario: Lookaround is an error
Given a row with lookaround
Then the compiler reports an error "uses lookaround, which the build check does not support yet"
Scenario: A block after a name must be regex or find
Given the code "x = pattern:"
Then the compiler reports an error "a block after `name =` is written `regex:` or `find:`, not `pattern:`"
export price = regex:
\$? dollar
\d{1,3}(?:,\d{3})+|\d+ amount
(?:\.\d{2})? cents
matches "$1,249.50" "42"
rejects "1,2345" "$"
The output is export const price = new RegExp(...), typed as RegExp in the .d.ts file.