DEV Community

Cover image for 6 Use Cases for Regex in Your Daily Work

6 Use Cases for Regex in Your Daily Work

Dennis Persson on November 27, 2022

In This Article What Is Regex? Regexes in a Nutshell When Is Regex Used? Searching Through Files, Manuals and Logs Regex Search in a...
Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

Check if a number is prime:

const isPrime = x=>!'1'.repeat(x).match(/^1?$|^(11+?)\1+$/)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
raibtoffoletto profile image
RaΓ­ B. Toffoletto

🀯

Collapse
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard

What formula is that? It's super intriguing.

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ • Edited

It is explained here - it is indeed very interesting

Thread Thread
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard

Mindblowing!

Collapse
 
schemetastic profile image
Schemetastic (Rodrigo)

Okay, I read it, and I must say it was an engaging reading that I enjoyed.

I think is a great intro for regex, I would usually think of regex more for data validation, replacement or maybe searching... but the examples here were practical.

I agree that regex comes very handy in many cases and sometimes there may be better approaches, but as a JS dev, validate and replace data with pure methods and conditionals sometime can get very verbose: indexOf(), includes(), a lot of if, replace, split, replace (again)... sometimes I would say it's necessary to use methods like these cause they provide readability to the code, but not always, in some circumstances regex can be more practical.

As you may remember I don't know too much about regex so this article is very useful for me. Currently I'm working on a small JS library in which I'm using a simple regex to replace 2 words, maybe I'll send you a link when I'll publish it.

Thanks for this article, should I put another end line? πŸ˜‚

Collapse
 
perssondennis profile image
Dennis Persson

Thanks for great feedback πŸ˜€

I agree that regex shouldn't be used if it isn't necessary. replace and include functions are to prefer if it can be done easily, readably and effectively :)

Ya, send me a link when it's published. Will be fun to read.

You can put another deadline at end of January I think. Cuz completely busy with other things now for the year out. Will publish some draft or small article in December and then regex one in January :)

Collapse
 
schemetastic profile image
Schemetastic (Rodrigo)

Hello!
I didn't forgot about this but up 'til now I published the library when I thought it was going to take less time, haha.

End of January? The last time i choosed the last day of November... so this time should be more challenging πŸ€” ... I'll put the reminder... to... January 30.

Screenshot of a calendar showing an appointment on the day 30

And about my small lib (that took me months to finish the initial release version), For sure I'll send you the link, where? I have an email to hello@schemetastic.com

Thread Thread
 
perssondennis profile image
Dennis Persson

Will be fun to see that lib :) You can send on LinkedIn (link in website) or email dennis@perssondennis.com

Haven't been able to write anything in December. But think I should be able to publish it in January :)

Collapse
 
schemetastic profile image
Schemetastic (Rodrigo)

You did it! And I didn't forget about this! I was waiting for this article! Actually I think it was today or yesterday when I remembered about this.

Image description

No doubt I'll read this, feedback will be provided soon!

Collapse
 
perssondennis profile image
Dennis Persson

Happy to see you here :D Wasn't sure you would actually read it, but I didn't want to miss the "deadline" πŸ˜„

Collapse
 
johnnyreilly profile image
John Reilly

Nice post! I've got a love / hate relationship with regex in that it's very powerful but I'll confess I can never remember the syntax! I found a use the other day, not sure it quite fits your 6 categories:

blog.johnnyreilly.com/2022/11/25/a...

It's basically extracting data from a string to drive further operations. In my case you could probably get there with .substring, but this ends up being far more reliable. Nice work anyway!

Collapse
 
fjones profile image
FJones

The thing about regex syntax is that people tend to overuse shorthands and complicated structures that they don't need or shouldn't use. In its simplest form, you just need one type of repetition (+ or *), alternation (|), character groups ([] and .), and parens for grouping (()). Everything else is just syntactic sugar or needless extended functionality (e.g. backreferences or readahead).

a* is just (a+|) (or vice-versa), a? is just (a|), and so on. Understanding how a regex would work with this limited instruction set makes the more advanced syntax a lot more palatable.