RegExp Learnings
RegExp is daunting and very powerful. These are a few things I picked up along the way.
Please take everything here with a grain of salt. It’s mostly to remind myself later and I’m by no means an expert on RegExp.
Positive Lookahead for Length Checks
Let’s say you want to test an input for proper emails (Everyone knows you should NOT yet we all do). You might have specific constraints on total length AND different patterns, you could use a pattern like this:
(?=.{1,64}$)[a-z0-9][a-z0-9.-]+\.[a-z]{2,}$
Explanation
(?=.{1,64}$) - This is the positive lookahead, that checks for a total length until the end of the line (notated by $). In this case it checks for a limit of 64 chars.
It works, because it lookaheads don’t don’t consume characters, like patterns do. So they first check the string, and return if they match or don’t. Then the patterns can validate again.
Can be handy sometimes!