zena:regex
import {…} from 'zena:regex';
Classes
Ast
final class Ast
Match
final class Match
Regex
final class Regex
Regular expression engine for pattern matching in strings.
This is a Thompson NFA-based engine (similar to RE2 and Go's regexp) that guarantees O(n*m) time complexity where n is input length and m is pattern size. It never backtracks exponentially, making it safe for untrusted input.
Basic Usage ​
let re = new Regex('\\d+'); // Match one or more digits
re.test('abc123'); // true
let m = re.exec('abc123'); // Match object with value='123'
Template Literal (Recommended) ​
Use the regex tag to avoid double-escaping backslashes:
import {regex} from 'zena:regex';
let re = regex`\d+`; // No double backslash needed
let email = regex`\w+@\w+\.\w+`;
Supported Features ​
- Literals:
abcmatches "abc" - Alternation:
cat|dogmatches "cat" or "dog" - Character classes:
[a-z],[^0-9],\d,\D,\w,\W,\s,\S - Quantifiers:
*(0+),+(1+),?(0-1),{n},{n,},{n,m} - Non-greedy:
*?,+?,??,{n,m}? - Groups:
(...)capturing,(?:...)non-capturing - Named groups:
(?P<name>...)withmatch.group('name') - Anchors:
^start,$end,\bword boundary,\Bnon-boundary - Any char:
.(any except newline), with(?s)matches newline too - Escapes:
\n,\t,\r,\.,\\, etc. - Flags:
(?i)case-insensitive,(?m)multiline,(?s)dot-all
Main API ​
test(input)- Returns true if pattern matches anywhere in inputexec(input, startIndex?)- Returns Match object or nullmatchAll(input)- Returns array of all Match objectsreplace(input, replacement)- Replace first matchreplaceAll(input, replacement)- Replace all matchessplit(input)- Split string around matches
Stateless Design ​
Unlike JavaScript's RegExp, this class is stateless - there is no lastIndex
property. Each method call is independent. To match from a specific position,
pass startIndex to exec():
let re = regex`\w+`;
let m1 = re.exec('foo bar', 0); // matches 'foo' at 0
let m2 = re.exec('foo bar', 4); // matches 'bar' at 4
Escaping in String Literals ​
When using new Regex(pattern), backslashes must be doubled because string
literals process escapes first:
new Regex('\\d+'); // Correct: matches digits
new Regex('\d+'); // Wrong: \d is not a valid string escape
The regex template tag avoids this by using raw strings.
new(pattern: String, flags: String = '')
test(input: String): boolean
exec(input: String, startIndex: i32 = 0): Match | null
matchAll(input: String): Array<Match>
replace(input: String, replacement: String): String
replace(input: String, replacer: (m: Match) => String): String
replaceAll(input: String, replacement: String): String
replaceAll(input: String, replacer: (m: Match) => String): String
split(input: String): Array<String>
Enums
Op
enum Op
Flags
enum Flags
Functions
parsePattern
function parsePattern(pattern: String, flags: Flags): Ast
regex
function regex(strings: TemplateStringsArray, values: FixedArray<String>)
Tagged template literal for creating Regex objects with raw strings.
Backslashes don't need escaping:
let re = regex\d+; // instead of Regex.compile("\d+")
let re = regex\w+@\w+; // instead of Regex.compile("\w+@\w+")