Regex. new Synchronous
Create a reusable regular-expression object from a pattern. Patterns use the Java regular-expression engine. Compile once and reuse the object for repeated searches, input validation, or text cleanup. Use anchors such as ^ and $ when the whole input must match.
Luau
Regex.new(pattern: string) -> RegexParameters
| Parameter | Type | Description |
|---|---|---|
pattern | string | Regular expression to compile. This is regex syntax, not a Luau string pattern. |
Returns
RegexA Regex object exposing Match, MatchMany, and Replace. Invalid patterns raise an error.
Usage notes
Backslashes must also be escaped in ordinary Luau strings. Long-bracket strings can make patterns easier to read.
Example
Luau
local digits = Regex.new("[0-9]+")
print(digits:Match("item 42")) -- 42