Flipped Coding

Regex…

learn more about 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,}))$/

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]+$/

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