How to Use RegEx
Have you ever needed to test whether a string contains a certain pattern? For simple applications this can be an easy task, but if you need to check for multiple patterns as once, RegEx will save lots of time.
Regular Expression (RegEx) can be used to match a pattern against a string. It can be thought of as a very powerful wildcard. It really isn’t anything more than a string of symbols with a meaning. It is very dynamic, allowing the developer to easily write the expression to be used for any application.
The following is an example of a regular expression:
/([A-Z])\w+/g
I know this block of code looks like someone just slammed their hands on a keyboard, but I promise it does something. It actually means that any word that contains an uppercase letter will be selected.
Each expression opens and closes with a forward slash. What is to be selected will be placed in the block in the center of these slashes. Outside the final forward slash, flags can be added. In this case, the “g” outside the slash means that it will be a global search. This means that the search will resume after the first match is found, otherwise it would stop after finding the first match.
/ BLOCK /
Another example:
/\b\w{4}\b/gThe above expression will select any word that contains 4 letters. Again, this example uses the global flag “g.”

Writing these expressions can be very confusing, but I found the cheatsheet to the left to be very helpful. This cheatsheet is from RegExr.com, a website that lets you to test your expression as you type.
Any combination of the characters to the left may be used in the block to select what you would like from the string.
I came across RegEx when working on a lab where I needed to split a string that had all kinds of punctuation. I wanted to split the string at either the period, question mark, or exclamation point. To do this I used a regular expression.
/\?|\!|\./g
The above code ended up working for me. I began with the forward slash and then a backslash for an escaped special character and then the question mark. I repeated that step for each symbol, connecting with the “|” symbol, which is regex’s version of “or.”
The final application for this expression was as follows:
string = "Hello! My name is Charlie. How are you today?"
string.split(/\?|\!|\./)
# => ["Hello", "My name is Charlie", "How are you today"]