regexp.ts 471 B

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