Regex.new Synchronous

Compile a Java regular expression once, then reuse it for searches or replacements. This is useful when you repeatedly inspect values with the same pattern, such as an identifier or a version string.

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

Parameters

Function parameters
ParameterTypeDescription
patternstringJava regular-expression pattern. This is not a Luau string pattern.

Returns

Regex

A Regex object with Match, MatchMany, and Replace methods. An invalid pattern raises an error.

Usage notes

Use ^ and $ when the entire input must match. In ordinary Luau strings, escape a backslash for Luau as well as for the regex; long-bracket strings can make backslash-heavy patterns easier to read.

Example

Example
Luau
local number = Regex.new("^[0-9]+$")
print(number:Match("482")) -- 482
print(number:Match("482px")) -- nil
Kawaii documentation