Skip to content

Regex

Built-in library for regular expressions

local Regex = require("@lune/regex")
local re = Regex.new("hello")
if re:isMatch("hello, world!") then
print("Matched!")
end
local caps = re:captures("hello, world! hello, again!")
print(#caps) -- 2
print(caps:get(1)) -- "hello"
print(caps:get(2)) -- "hello"
print(caps:get(3)) -- nil

Creates a new Regex from a given string pattern.

This constructor throws an error if the given pattern is invalid.

  • pattern string The string pattern to use
  • Regex The new Regex object

Check if the given text matches the regular expression.

This method may be slightly more efficient than calling find if you only need to know if the text matches the pattern.

  • self Regex

  • text string The text to search

  • boolean Whether the text matches the pattern

Finds the first match in the given text.

Returns nil if no match was found.

  • self Regex

  • text string The text to search

  • RegexMatch? The match object

Finds all matches in the given text as a RegexCaptures object.

Returns nil if no matches are found.

  • self Regex

  • text string The text to search

  • RegexCaptures? The captures object

Splits the given text using the regular expression.

  • self Regex

  • text string The text to split

  • { string } The split text

Replaces the first match in the given text with the given replacer string.

  • self Regex

  • haystack string The text to search

  • replacer string The string to replace matches with

  • string The text with the first match replaced

Replaces all matches in the given text with the given replacer string.

  • self Regex

  • haystack string The text to search

  • replacer string The string to replace matches with

  • string The text with all matches replaced

A match from a regular expression.

Contains the following values:

  • start — The start index of the match in the original string.
  • finish — The end index of the match in the original string.
  • text — The text that was matched.
  • len — The length of the text that was matched.

Captures from a regular expression.