@gruhn/regex-utils
    Preparing search index...

    Function enumerate

    • A generator function that returns a (potentially infinite) stream of strings that match the given RegExp. This can be useful for testing regular expressions.

      import { enumerate } from '@gruhn/regex-utils'

      const emailRegex = /^[a-z]+@[a-z]+\.[a-z]{2,}$/

      for (const matchedStr of enumerate(emailRegex)) {
      console.log(matchedStr)
      }
      a@a.aa
      b@a.aa
      aa@a.aa
      c@a.aa
      ba@a.aa
      a@b.aa
      d@a.aa
      ca@a.aa
      b@b.aa
      ab@a.aa
      e@a.aa
      da@a.aa
      c@b.aa
      bb@a.aa
      aa@b.aa
      f@a.aa
      ea@a.aa
      d@b.aa
      cb@a.aa
      ba@b.aa
      a@aa.aa
      g@a.aa
      ...
      Warning

      If the regular expression matches infinitely many strings then a loop like above won't terminate.

      Tip

      Use the new Iterator helpers to only get the first N matches, e.g enumerate(emailRegex).take(100).

      The generator produces a fair enumeration. That means every string that matches the regular expression is eventually enumerated. To illustrate, an unfair enumeration of /^(a+|b+)$/ would be:

      "a", "aa", "aaa", "aaaa", "aaaaa", ...
      

      because it never produces any strings of b's. A possible fair enumeration is:

      "a", "b", "aa", "bb", "aaa", "bbb", "aaaa", "bbbb", ...
      

      Parameters

      • re: RegExp

      Returns Generator<string>