The Better Way to Write Regex

preview_player
Показать описание
RESOURCES

Get early access to videos, exclusive content, interactive code challenges, and more.

follow me on twitter:

#vuejs #vue #javascript #webdev
Рекомендации по теме
Комментарии
Автор

While I'm really used to writing regex, the type safety is a real bonus with this library.

soviut
Автор

I once thought regex was hard as well. But then I realized "wait a minute, I've never even put the slightest amount of effort into learning this thing". I guess I would think Python was hard as well if I was trying to come up with a calculator app without even having a grasp of what a variable is meant to be.
After that realization I dedicated about an hour to learning regex, and suddenly it turned into the simplest thing (who would have thought, ha?). And after that, I got excited and later decided to dedicate about a day to learning "almost everything" there is to know about regex, just for the heck of it, and then I never had a single problem or frustration with it again.

sergio_henrique
Автор

yeah daniel nailed with this one. This should be the standard

khairulhaaziq
Автор

As someone who sometimes has to write regular expressions that almost reaches arcane magics, this would be a blessing for myself and my colleagues.

CottidaeSEA
Автор

It is an useful library, specially for those who are beggining, because of the similarities with the natural language. But even if it is more difficult to learn the "traditional" regex, I find ir more simple, after you have learnt it. And it means you save much more time with traditional.

ESPjump
Автор

I am not that elaborate user or complex regexes, but I thought of a similar manner, that I hope you find helpful implementing in your own language.
So I'll use this string as a regex definition (with which I mean I would use to construct the regex, not the actual regex string): [for readability purpose, assume quotes do encapsulate your language's multilines strings"]
" this string is
just

this"
is really just this regex:
"this string is just this"
And this:
"this space string space is newline just newline tab this"
is just the regex:
"this string is
just
this"
While the string:
" letter+ plus letter + equals digit +"
So this string actually means:
"[a-zA-Z]+\+[a-zA-Z]+ = \d+"
What I would do is parse arbitrary keywords I chose before like space, tab, newline, plus, dot, letter, word for their regex equivalent, and be escaped by a \. So I would first parse:
"this space \space space is space empty"
doesn't mean:
"this is empty", with 3 spaces, rather:
"this space is empty" where the \keyword is parse, and then keyword itself.

I didn't implement it.. Actually, didn't write a single line of it for the 2 weeks I was thinking about it. But what I'm currently thinking about is, the special operators like lookaheads and negative lookbehinds, capturing groups, naming groups, some not-so-obvious regexes (like how the ? means maybe but may be confused as a genuine question mark, unlike a + which is not used much in word context.

A better addition would be to add a console print that colors tokens differently, mostly grey for white spaces, but that would be IDE dependent I think.

But you get the hint.

reda
Автор

This is amazing.

Hopefully some kind soul will write a version of this for other languages such as Python or C++, it would be a godsend.

MyriadColorsCM
Автор

Vue/Nuxt core team authored so many useful packages that are not even limited to Vue/Nuxt!
They improve the whole front end ecosystem

ralacerda
Автор

I don't think regexes are that hard but this is really great. Reminds me of zod but for regex.

quintencabo
Автор

Wow. No joke, I wrote some code that does almost exactly the same thing as this just a few weeks ago, including the type inference for the whole match and each matched group. I didn't publish mine though, and this is probably a little more featureful and well-tested than mine. Mine also has to be written in a more functional style as opposed to dot chained method calls, so this is a little more ergonomic.

I did the same thing with Zod. Before I discovered Zod, I had implemented something similar myself multiple times. It's cool to see the open source community affirming my intuitions by independently arriving at a similar design to what I have, myself. I will definitely be using this.

joshuakb
Автор

Using the "x" flag to allow embedded comments in your regular expressions can make a HUGE difference in their future readability. I make heavy use of it any time I'm doing anything non-trivial with regexes.

djdawso
Автор

0:21 OH GOD, that is horrendous to read..
I think I'l stick to reading regex directly

ColinRichardson
Автор

Absolutely great auxiliary library, it mitigates so much overhead of writing regex 💯

ayushdedhia
Автор

Duuuude, the type safety is BEAUTIFUL. Absolutely gonna use this in the future.

cfuendev
Автор

Cool library! However I'm not convinced that this better than just learning the regex syntax, which is a skill that's largely transferable to any language.

kezzu
Автор

Oh wow, did not know this existed! Will 100% add this to my toolkit. Thank you.

codybontecou
Автор

Where the heck was this vid earlier today before spending hours with the usual trial and error? You sir, have just introduced me to my new favorite tool~

CyanureNeko
Автор

Amazing! this is the kind of thing I didn't know I needed 😎 thanks for sharing mate!

ricko
Автор

The fact that this is all type safe makes it 1000x better

maxijonson
Автор

Exactly what I needed! and was actually close to creating own library, but I'm glad that finally someone created that 😅

DEVDerr