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'
constemailRegex = /^[a-z]+@[a-z]+\.[a-z]{2,}$/
for (constmatchedStrofenumerate(emailRegex)) { console.log(matchedStr) }
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 generator function that returns a (potentially infinite) stream of strings that match the given
RegExp
. This can be useful for testing regular expressions.If the regular expression matches infinitely many strings then a loop like above won't terminate.
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:because it never produces any strings of b's. A possible fair enumeration is: