From c51e4941427bf8f994784e225c3f8fbf7b603040 Mon Sep 17 00:00:00 2001 From: RebornEnder Date: Thu, 13 Nov 2025 18:57:16 +0100 Subject: [PATCH] Add execAll method to RegExp type Added execAll method to RegExp type for matching all occurrences in a string. --- src/Regexp.global.lua | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Regexp.global.lua b/src/Regexp.global.lua index fa1e802..c45fa85 100644 --- a/src/Regexp.global.lua +++ b/src/Regexp.global.lua @@ -4,8 +4,9 @@ type Array = { [number]: T } type RegExpExecArray = Array & { index: number?, input: string?, n: number } export type RegExp = { - exec: (self: RegExp, input: string) -> RegExpExecArray | nil, - test: (self: RegExp, input: string) -> boolean, + exec: (self: RegExp, input: string) -> RegExpExecArray | nil, + execAll: (self: RegExp, input: string) -> {RegExpExecArray}, + test: (self: RegExp, input: string) -> boolean, } local RegExp = {} @@ -35,6 +36,31 @@ function RegExp:exec(str: string): RegExpExecArray | nil return matches end +function RegExp:execAll(str: string): {RegExpExecArray} + local matchFunction = self._innerRegEx:matchall(str) + local finds = {} + + while task.wait() do + local match = matchFunction() + if not match then + break + end + + local index = match:span() + local groups = match:grouparr() + + local matches = { groups[0] } + for i = 1, groups.n do + matches[i + 1] = groups[i] + end + matches.n = groups.n + 1 + matches.index = index + matches.input = str + table.insert(finds, matches) + end + return finds +end + function RegExp:test(str: string): boolean return self:exec(str) ~= nil end