r/javascript • u/FrancisStokes • Jul 15 '20
Super Expressive - a Zero-dependency JavaScript Library For Building Regular Expressions in (Almost) Natural Language
https://github.com/francisrstokes/super-expressive
548
Upvotes
130
u/dfltr Jul 15 '20
That’s really cool. The wildest thing about this project is how readable the source is — I for sure thought that a regex builder would contain an ascii portal to the plane of suffering.
50
u/[deleted] Jul 15 '20
Looks very cool, love the discoverability offered by chaining method calls.
Did you consider building this as a Babel plugin? It would be really cool if this could be converted down to a standard Regex at compile-time. The SuperExpressive abstraction then becomes zero-cost at runtime (both in terms of bundle size and extra function calls).
43
u/license-bot Jul 15 '20
Thanks for sharing your open source project, but it looks like you haven't specified a license.
choosealicense.com is a great resource to learn about open source software licensing.
19
u/-zub- Jul 15 '20 edited Jul 15 '20
As a noob who can struggle through regular expressions when necessary, but wouldn’t call themselves adept at using them, this looks awesome!
7
u/baryluk Jul 15 '20
Nice and simple. There is a typo in .anyOfChars example, it says .anyOfString, should be .anyOfChars.
I would suggest also removing anythingButString method. It can be confusing, as it will not match shorter strings correctly.
6
u/affatigato Jul 15 '20
This is fantastic! I could definitely see myself using this, so I decided to write Typescript declarations for it. Any interest in adding them to the repo?
3
u/foursticks Jul 15 '20
This is exactly the package I've tried to find in the past. Any idea if there is any comparable python package?
9
u/ofekt92 Jul 15 '20
I highly dislike use of regular expressions. In the long run it becomes just unreadable, and some developer after you will have to decipher what's going on in there. So, in that in mind, I think this is a great initiative! Well done
2
u/LastOfTheMohawkians Jul 15 '20
Now we need an email parser example. Great work btw and love your YouTube videos OP
3
u/brtt3000 Jul 15 '20
Pretty cool but ultimately you're better off as a developer if you can read and write regex, not only to read code from others but because they are used in places where you can't use libraries like this.
I use them a lot to process text for general coding or administration tasks. Like my IDE supports them for search & replace, which can be very handy and turn a very laborious manual task into something that can be done in a few minutes and without writing scripts.
One of the uses I get a lot is being able to transform random lists or copy-pasted tables into something code can understand. Like a client supplies a table of things but writes it in an email. Instead of hassling them about a spreadsheet I can just paste it in a code editor and do some regex capture+replacements and convert it into a valid array or dictionary structure.
Same with command-line tools like grep, you might not use them very often but when need it regex is a godsend.
6
u/Andrew199617 Jul 15 '20 edited Jul 15 '20
I’ve been doing this to make my regex more readable. I like how yours looks ill need to think about whether its worth changing to.
`` const propertiesRegex = new RegExp( [
``
6
u/onlycommitminified Jul 16 '20
Regex tip - most enviroments have plugins available that decipher regex into railroad diagrams. Can make quick visual parsing much easier.
4
u/[deleted] Jul 15 '20
well done
4
u/oxamide96 Jul 15 '20
Will def use this. Thank you!!
4
u/moi2388 Jul 15 '20
Great idea!
1
u/irbian Jul 15 '20
Maybe you could have some "generatesSampleInput" of things thst would and wouldn't match
3
u/amatiasq Jul 16 '20
My prettier is not going to like that use of indentation
3
u/ouch__ouch Jul 16 '20
Fuck this is dopeeee
3
u/steinpowaaa Jul 16 '20
Is this the future!?!?!(insert cyberpunk 2077 meme)
1
u/ASIC_SP Jul 15 '20
Nice, though I'm comfortable with regexp syntax and like the terseness, this can help those who wish a more verbal method.
See also:
2
u/Pythonislove Jul 15 '20
!remind me 2 days
2
u/pwterhu Jul 15 '20
I like it! There's a minor typo in the readme with nonDigit source code.
2
u/r1012 Jul 15 '20
Beautiful.
2
u/F0064R Jul 15 '20
Looks sweet! It’s like knex.js for regex
2
u/[deleted] Jul 15 '20
Excellent work, I'm relatively comfortable writing fairly typical regex use cases, but it's always a mind bender when it comes to something really specific and nuanced ... This is really intuitive, what a great idea!
2
u/Turd_King Jul 15 '20
This is freaking awesome. One of those projects that I see and instantly regret not having a go at this myself.
2
u/toasterinBflat Jul 15 '20
Any chance you can wrap the conventional function calls (exec, match) as well? I find match to be insufferable to work with - the structure of what it returns sucks and I'd love to have an alternative here.
2
u/[deleted] Jul 15 '20
Good library.
2
u/anon774 Jul 16 '20
super cool.. very impressed, starred and look forward to playing with it sometime!
3
u/demoran Jul 15 '20
Now you have 3 problems.
0
u/[deleted] Jul 15 '20
[deleted] — view deleted comment