From 6319f810b5a0677fd8dedb5d76d1863a14ca0a69 Mon Sep 17 00:00:00 2001 From: ztimson Date: Wed, 29 Jul 2026 17:42:13 -0400 Subject: [PATCH] Added safty guardrails to matchAll --- src/string.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/string.ts b/src/string.ts index e75303e..7a2bba0 100644 --- a/src/string.ts +++ b/src/string.ts @@ -280,19 +280,16 @@ function titleCase(str: string) { * @return {RegExpExecArray[]} Found matches. */ export function matchAll(value: string, regex: RegExp | string): RegExpExecArray[] { - if(typeof regex === 'string') { - regex = new RegExp(regex, 'g'); - } - - // https://stackoverflow.com/a/60290199 - if(!regex.global) { - throw new TypeError('Regular expression must be global.'); - } + if(typeof regex === 'string') regex = new RegExp(regex, 'g'); + if(!regex.global) throw new TypeError('Regular expression must be global.'); let ret: RegExpExecArray[] = []; let match: RegExpExecArray | null; while((match = regex.exec(value)) !== null) { ret.push(match); + if(match[0].length === 0) { + regex.lastIndex++; + } } return ret;