Regex.Escape Synchronous

Make user-provided text safe to place inside a regular expression as a literal. Punctuation such as dots and brackets will match itself instead of gaining regex meaning.

Syntax
Luau
Regex.Escape(text: string) -> string

Parameters

Function parameters
ParameterTypeDescription
textstringThe literal text to match.

Returns

string

A regex fragment that matches precisely that text.

Usage notes

Escape only the literal piece; you can still add your own anchors or pattern syntax around it.

Example

Example
Luau
local filename = "settings.json"
local exact = Regex.new("^" .. Regex.Escape(filename) .. "$")
print(exact:Match("settings.json")) -- settings.json
print(exact:Match("settingsXjson")) -- nil
Kawaii documentation