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.
Luau
Regex.new(pattern: string) -> RegexParameters
| Parameter | Type | Description |
|---|---|---|
pattern | string | Java regular-expression pattern. This is not a Luau string pattern. |
Returns
RegexA 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
Luau
local number = Regex.new("^[0-9]+$")
print(number:Match("482")) -- 482
print(number:Match("482px")) -- nil