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