regex
The regex module provides regular expression matching, replacement, and splitting.
Patterns use Rust’s regex syntax (similar to PCRE without backreferences).
Import
Section titled “Import”use std::core::regexFunctions
Section titled “Functions”regex::is_match(text: string, pattern: string) -> bool
Section titled “regex::is_match(text: string, pattern: string) -> bool”Test whether the pattern matches anywhere in the text.
regex::is_match("hello world", "\\bworld\\b") // trueregex::is_match("hello world", "^\\d+$") // falseregex::find(text: string, pattern: string) -> object?
Section titled “regex::find(text: string, pattern: string) -> object?”Find the first match of the pattern. Returns a match object or None if
no match is found. (The name is find rather than match because match is
a Shape keyword.) The match object has:
| Field | Type | Description |
|---|---|---|
text | string | The matched text |
start | number | Start byte offset |
end | number | End byte offset |
groups | Array<string?> | Capture group values (excluding the full match) |
let m = regex::find("abc 123 def", "(\\d+)")// m == Some({ text: "123", start: 4, end: 7, groups: ["123"] })regex::match_all(text: string, pattern: string) -> Array<object>
Section titled “regex::match_all(text: string, pattern: string) -> Array<object>”Find all non-overlapping matches of the pattern. Returns an array of match objects.
let matches = regex::match_all("a1 b2 c3", "\\d")// matches.length == 3regex::replace(text: string, pattern: string, replacement: string) -> string
Section titled “regex::replace(text: string, pattern: string, replacement: string) -> string”Replace the first match of the pattern with the replacement string. Use $1,
$2, etc. to reference capture groups in the replacement.
regex::replace("foo bar foo", "foo", "baz")// "baz bar foo"regex::replace_all(text: string, pattern: string, replacement: string) -> string
Section titled “regex::replace_all(text: string, pattern: string, replacement: string) -> string”Replace all matches of the pattern with the replacement string.
regex::replace_all("2024-01-15", "(\\d{4})-(\\d{2})-(\\d{2})", "$3/$2/$1")// "15/01/2024"regex::split(text: string, pattern: string) -> Array<string>
Section titled “regex::split(text: string, pattern: string) -> Array<string>”Split the text at each match of the pattern.
regex::split("hello world test", "\\s+")// ["hello", "world", "test"]
regex::split("one,two,,three", ",")// ["one", "two", "", "three"]