aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/regexp.ts
blob: cfc2be4882523e7c5dbd20c56903801a059b0df6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Thanks to https://regex101.com
function regexpCapture (str: string, regex: RegExp, maxIterations = 100) {
  const result: RegExpExecArray[] = []
  let m: RegExpExecArray
  let i = 0

  // tslint:disable:no-conditional-assignment
  while ((m = regex.exec(str)) !== null && i < maxIterations) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
      regex.lastIndex++
    }

    result.push(m)
    i++
  }

  return result
}

export {
  regexpCapture
}