slug.regex
slug.regex — regular expression matching and extraction
All functions accept standard regex syntax. Patterns are not compiled or cached — for performance-sensitive code with repeated matches on the same pattern, consider pre-processing outside hot paths.
Patterns use standard Go/RE2 syntax (no lookaheads or backreferences).
TOC
findAll(str, pattern)findAllGroups(str, pattern)indexOf(str, pattern, index)matches(str, pattern)replaceAll(str, pattern, repl)split(str, pattern)
Functions
findAll(str, pattern)
fn slug.regex#findAll(str:str, pattern:str):list<str>
returns a list of all non-overlapping matches of pattern in str.
| Parameter | Type | Default |
|---|---|---|
str | str | — |
pattern | str | — |
Examples
findAll("1|2|3", "\d+") // => ["1", "2", "3"]
findAll("(foo)", "[a-z]+") // => ["foo"]
findAllGroups(str, pattern)
fn slug.regex#findAllGroups(str:str, pattern:str):list<list<str>>
returns a list of all matches including capture groups.
Each entry is a list where the first element is the full match and subsequent elements are the capture group values.
| Parameter | Type | Default |
|---|---|---|
str | str | — |
pattern | str | — |
Examples
findAllGroups("<a href="foo">bar</a>", "<a href="(.*?)">(.*?)</a>") // => [["<a href="foo">bar</a>", "foo", "bar"]]
indexOf(str, pattern, index)
fn slug.regex#indexOf(str:str, pattern:str, index:num = 0):[num, num]|nil
returns the byte index range of the first match of pattern in str starting from index, as [start, end].
Returns nil if no match is found. The list contains the start and end byte indices of the full match.
| Parameter | Type | Default |
|---|---|---|
str | str | — |
pattern | str | — |
index | num | 0 |
matches(str, pattern)
fn slug.regex#matches(str:str, pattern:str):bool
returns true if the pattern matches anywhere in the string.
| Parameter | Type | Default |
|---|---|---|
str | str | — |
pattern | str | — |
Examples
matches("test", "\d+") // => false
matches("aaabbb", "b+") // => true
matches("123", "\d+") // => true
replaceAll(str, pattern, repl)
fn slug.regex#replaceAll(str:str, pattern:str, repl:str):str
replaces all matches of pattern in str with repl.
Use $1, $2 etc. in repl to reference capture groups.
| Parameter | Type | Default |
|---|---|---|
str | str | — |
pattern | str | — |
repl | str | — |
Examples
replaceAll("1|2|3", "\d+", "x") // => "x|x|x"
split(str, pattern)
fn slug.regex#split(str:str, pattern:str):list<str>
splits str into a list of substrings divided by matches of pattern.
| Parameter | Type | Default |
|---|---|---|
str | str | — |
pattern | str | — |
Examples
split("1|2|3", "\|") // => ["1", "2", "3"]