DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Regex…
Milecia
Milecia

Posted on • Updated on

Regex…

Regular expressions are one of the most powerful, but complicated things you can use in web development. They are pretty cryptic by nature and it can take a while to figure out what they are really doing. That's why it's important you understand how they work before you end up with a file that you can't use.

To start, regular expressions are typically used for string manipulation, validations, and most importantly, searches. They help you go through your string values quickly and get the information you need from them with the accuracy of a sniper. Don't get me wrong, they aren't the answer to all of your problems. There's definitely a time and place to use regular expressions.

A common use of regular expressions is in email validation. Since you're dying to see what this would look like, here you go:

/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
Enter fullscreen mode Exit fullscreen mode

This regular expression meets all of those security requirements set by the internet people. Don't be intimidated by this. It would take an experienced web developer to read through that string with any kind of understanding. Keep in mind that this is just the regular expression for JavaScript. If you were doing it in another programming language like C#, you might need to format the regex differently.

One of the most powerful uses of regular expressions is their ability to make searches faster. You can comb through millions of strings and find the exact information you need if you can write a good regular expression.

It's one of the reasons Google can find what you're looking for so fast. Plus, there are different regular expressions that will return the same results. That means regular expressions can be written to search more efficiently.

You can use regex to find any combination of numbers and letters. If you need to handle dates, passwords, IP addresses, or URLs, regular expressions can handle those too. They can help you with web scraping or just plain old validation of form inputs. Here's a couple of simple regular expressions used for every day coding matters:

Numbers: /^[0-9]+$/

Letters: /^[a-zA-Z]+$/

Numbers and letters: /^[a-zA-Z0-9]+$/
Enter fullscreen mode Exit fullscreen mode

Honestly, regular expressions are kind of magic. The fact that you can put these seemingly random characters together and make something out of them is crazy. I'm still trying to wrap my head around how regular expressions really work. They do start to make sense in a weird way after you see them enough though.


Hey! You should follow me on Twitter because reasons: https://twitter.com/FlippedCoding

Top comments (3)

Collapse
 
somedood profile image
Basti Ortiz • Edited on

You should practice your skills at regex101. It's an amazing site I've been using for quite a while now to practice my RegExp skills.

To really immerse yourself in learning, my advice is to have an actual way of applying your newfound knowledge. For example, there was this one time when I had to format a string into a sentence. The catch was that I had to format a whole bunch of data into strings. So instead of manually typing the string or copying-and-pasting the data from the CSV I was supposed to formatβ€”something my peers would have doneβ€”I chose to make my life easier by automating the formatting process with RegExp. I blazed through the task while everyone manually punched in the data.

EDIT: Just realized now that I could have just used String#split... πŸ€¦β€β™‚οΈ

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

Classic DEV post:

CLI tools