Using regular expressions

Michael Abe
3 min readNov 11, 2021

When we look at coding, as in nature and life one can start to see patterns and predictability. Coding in general uses a lot of patterns that we adjust to to suit different needs. On example of this is using something like a for loop so much that we basically have built in functions to use that functionality since it is so commonly used. When we expand that idea outside of coding itself, we can think about many examples of identifying some repeating patterns. When we have text inputs on websites sometimes it would be a good idea to do a quick check to see if the input meets the patterns that we would see in the input that we are asking for. One major example of a pattern that we see when dealing with a specific input is in an email address. The immediate way to tell if an email address is valid is by making sure that it fits the standard email format.

Standard email format is split into what is known as the ‘local-part’ which is the unique user generated characters part of the email address and the domain. The two parts of the email are split with the use of the ‘@’ sign. This means that there can only be one instance of that sign in an email. As an example ‘user@email.com’ would be a valid format while “other@user@gmail.com’ would not be allowed since the ‘@’ symbol can not exist in the local part of the email address. Ultimately there are a multitude of reasons why something can and can not be a valid email address.

A great way to see if an input follows the pattern that is required to be a valid email address is to use regular expressions to determine that it is within the allowed format. Regular expressions are defined as “a sequence of characters that forms a search pattern”. We can alter the parameters of the search pattern to define exactly what we are looking for. “Regular expressions can be used to preform all types of text search and text replace operations”

The above code is a snip off of stack overflow what has been written by someone to determine the syntax of input to determine if it is the proper format to be an email address (note: this can’t determine if the email address actually exists or not, this merely eliminates inputs that are not even possible in the rules of email syntax). Coding can easily be confusing to look at but regular expressions can look particularly difficult. Here is a link to a ‘regular expression cheat sheet’ I would definitely suggest looking further into that to make some sense of it but we will take a look at a much more simple example of a regular expression.

The example above is a very simple example where we are searching the text variable for the word “confusing” and if we find it we are going to replace that word with “COOL!!” as we can see when we log the variable ‘result’ to the console we have the amended statement. This is a very simple and effective way to substitute or check strings.

--

--