regexp.ts 515 B

1234567891011121314151617181920212223
  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. // tslint:disable:no-conditional-assignment
  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. result.push(m)
  13. i++
  14. }
  15. return result
  16. }
  17. export {
  18. regexpCapture
  19. }