]>
Commit | Line | Data |
---|---|---|
f7cc67b4 C |
1 | // Thanks to https://regex101.com |
2 | function regexpCapture (str: string, regex: RegExp, maxIterations = 100) { | |
a1587156 | 3 | const result: RegExpExecArray[] = [] |
f7cc67b4 C |
4 | let m: RegExpExecArray |
5 | let i = 0 | |
f7cc67b4 | 6 | |
f7cc67b4 C |
7 | while ((m = regex.exec(str)) !== null && i < maxIterations) { |
8 | // This is necessary to avoid infinite loops with zero-width matches | |
9 | if (m.index === regex.lastIndex) { | |
10 | regex.lastIndex++ | |
11 | } | |
12 | ||
13 | result.push(m) | |
14 | i++ | |
15 | } | |
16 | ||
17 | return result | |
18 | } | |
19 | ||
20 | export { | |
21 | regexpCapture | |
22 | } |