aboutsummaryrefslogblamecommitdiffhomepage
path: root/server/helpers/regexp.ts
blob: 257054cead9c076744a0ac18266c110d5c306024 (plain) (tree)
1
2
3
4
5
6

                                                                          
                                      

                        
 















                                                                        
// Thanks to https://regex101.com
function regexpCapture (str: string, regex: RegExp, maxIterations = 100) {
  const result: RegExpExecArray[] = []
  let m: RegExpExecArray
  let i = 0

  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
}