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.

Syntax
Luau
Regex.new(pattern: string) -> Regex

Parameters

Function parameters
ParameterTypeDescription
patternstringRegular expression to compile. This is regex syntax, not a Luau string pattern.

Returns

Regex

A 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

Example
Luau
local digits = Regex.new("[0-9]+")
print(digits:Match("item 42")) -- 42
Kawaii documentation