RegExp Class
RegExp is Alia's built-in regular expression class, used for pattern matching, text search, and replacement operations.
Creating Regular Expressions
Pattern Only
const re = new RegExp ['\\d+']
Pattern with Flags
const re = new RegExp ['hello', 'gi']
From an Object
const re = new RegExp [*[pattern: '\\w+', flags: 'i']]
Creating an Empty Regex
const re = new RegExp
Instance Properties (Read-Only)
| Property | Type | Description |
|---|---|---|
pattern | String | The regex pattern string |
flags | String | The flags string |
global | Boolean | Whether the g flag is set |
multiline | Boolean | Whether the m flag is set |
ignoreCase | Boolean | Whether the i flag is set |
unicode | Boolean | Whether the u flag is set |
sticky | Boolean | Whether the y flag is set |
dotAll | Boolean | Whether the s flag is set (. matches newlines) |
Instance Methods
test
Tests whether a string matches the regex. The signature is re.test input; it takes a single string argument and returns a Boolean.
const re = new RegExp ['\\d+']
print re.test 'abc123' ~ true
print re.test 'abc' ~ false
exec
Performs a matching search. The signature is re.exec input; it takes a single string argument, returning Null when there is no match and a details object on a match.
const re = new RegExp ['(\\d+)-(\\d+)']
const m = re.exec '2024-01-15'
if typeOf m == 'Object' {
print m.index ~ 0
print m.matched ~ '2024-01'
print m.groups ~ ['2024', '01']
}
Return value structure: *[index: Number, matched: String, groups: Array]
match
The signature is re.match input; it takes a single string argument and returns the match results, or Null when there is no match.
Non-global mode returns a nested single-element array:
const re1 = new RegExp ['\\d+']
print re1.match 'a12b' ~ [[*[index: 1, matched: '12', groups: []]]]
Global mode returns all matches:
const re2 = new RegExp ['\\d+', 'g']
print re2.match 'a12b34' ~ [*[index: 1, matched: '12', groups: []], *[index: 4, matched: '34', groups: []]]
replace
Replaces matched content. The signature is re.replace input with replacement; it takes the input string joined to the replacement string with with and returns a String.
const re = new RegExp ['\\d+']
print re.replace 'a12b34' with 'X' ~ 'aXb34'
const re2 = new RegExp ['\\d+', 'g']
print re2.replace 'a12b34' with 'X' ~ 'aXbX'
split
Splits a string by the pattern. The signature is re.split input; it takes the input string and returns an Array.
const re = new RegExp [',\\s*']
print re.split 'a, b, c' ~ ['a', 'b', 'c']
The number of returned results can be limited by attaching limit with an upper bound:
const re = new RegExp [',\\s*']
print re.split 'a, b, c, d' limit 2 ~ ['a', 'b']
Static Methods
RegExp.escape
Escapes regex special characters in a string. The signature is RegExp.escape input; it takes a single string argument and returns a String.
const escaped = RegExp.escape 'price: $10.00'
print escaped ~ 'price: \$10\.00'
Complete Example
~ Create regexes
const email = new RegExp ['[\\w.]+@[\\w.]+\\.[a-z]{2,}', 'i']
const digits = new RegExp ['\\d+', 'g']
~ Test matching
print email.test 'user@example.com' ~ true
~ Replace
print digits.replace 'abc 123 def 456' with '#' ~ 'abc # def #'
~ Split
const sep = new RegExp [',\\s*']
const parts = sep.split 'a, b, c'
print parts ~ ['a', 'b', 'c']
~ Match
const matcher = new RegExp ['\\d+', 'g']
const nums = matcher.match 'a12b34'
print nums ~ [*[index: 1, matched: '12', groups: []], *[index: 4, matched: '34', groups: []]]
~ exec
const re = new RegExp ['(\\d+)-(\\d+)']
const m = re.exec '2024-01-15'
if typeOf m == 'Object' {
print m.index ~ 0
print m.matched ~ '2024-01'
print m.groups ~ ['2024', '01']
}
~ Properties
print email.global ~ false
print email.ignoreCase ~ true
print email.pattern ~ '[\\w.]+@[\\w.]+\\.[a-z]{2,}'
~ Static methods
print RegExp.escape '$10.00' ~ '\\$10\\.00'
Flag Reference
| Flag | Property name | Description |
|---|---|---|
g | global | Global matching |
i | ignoreCase | Case-insensitive matching |
m | multiline | Multiline mode |
s | dotAll | . matches newlines |
u | unicode | Unicode mode |
y | sticky | Sticky matching |