4 ms·
It might be nice to touch on composition as a good way to get started is to test out individual pieces and be confident they work when you're putting them toget
by ben509 6y ago
It might be nice to touch on composition as a good way to get started is to test out individual pieces and be confident they work when you're putting them together.
If you're building a complex regular expression, setting smaller parts in variables and dropping them in with (?:${part}) makes things a bit more readable.
It also exposes a real weakness of most regex engines. In particular, alternation is a first-class operation, but complement and intersection, while theoretically possible[1] are typically not.
A person might guess that to match three keywords is /.keyword1.&.keyword2.&.keyword3./
Or maybe /.keyword1.&(.keyword2.)!/ to match keyword1 and not keyword2.
But those won't work, so it's a good idea to explain some options, an obvious one being /keyword1/.test() && !/keyword2/.test()
In the section on lookaround assertions, it's probably useful to note that (?=thing1)(?=thing2) can match both, and it's a good mental model for it, but that it comes with a few gotchas.
[1]: https://www.researchgate.net/publication/220994310_Succinctness_of_the_Complement_and_Intersection_of_Regular_Expressions https://www.researchgate.net/publication/220994310_Succinctn...