Image for post
Image for post
Photo by Pankaj Patel on Unsplash

Introduction

In my last blog post, I wrote about using switch statements when I don't need complicated if-else statements. It makes my code easier to read and overall just easier to manage. In keeping up with trying to clean up my conditional statements, I have been implementing more ternary operations into my code. Now I have usually avoided them just because I was just used to writing if-else statements, but honestly, less is more and if you don't need to type out a multiline statement, why not use a ternary operator? Let me show you why they are great!

To write a ternary…


Image for post
Image for post
Photo by Isabella and Louisa Fischer on Unsplash

Introduction

Something I am trying to get in the habit of using when I do not need complicated if-else statements are switch statements. Switch statements are great for comparing multiple options such as in the case of checking if a string matches a certain word or number. One example that comes to mind is a card game. You can check if your value is equal to a “J”, “K”, etc., and depending on the match, have the switch statement do something. Let’s get started on this tutorial.

Code

For the switch statement to work, we have to write the keyword switch and inside of the parenthesis, we give it an argument that we want to be compared. The switch statement works with strict equality so if you are expecting to compare values in the way that you would use == , I would go ahead and use a regular if-else statement.


Image for post
Image for post
Photo by Jason Leung on Unsplash

Introduction

I ran into a problem while doing a project on trying to convert currency. It wasn't the issue of doing the conversion, but what if I wanted the numbers to be separated by commas or periods? In the US, for the price of five thousand dollars, it is common to write it like $5,000.00, but in another country, it might be written like €5.000,00. I will show you how you can go about doing that in two ways.

Number.prototype.toLocaleString()

The .toLocaleString() method is a simple one in that it takes one argument with options.

  1. Locales [,options]

MDN notes:

When formatting large numbers of numbers, it is better to create a NumberFormat object and use the function provided by its NumberFormat.format


How matchAll is Compare and Contrast’s match

Lit matches
Lit matches
Photo by Jamie Street on Unsplash.

While I take a break from my RegEx series, I wanted to write an article that was still related to regular expressions. I recently learned about the .matchAll() method, which I had not used until this past week.

I had mentioned in this article how to match a pattern and extract it into an array using the .match() method. You’ll see that .matchAll() works similarly to .match() with slight differences.

.match()

The .match() method takes one argument: RegExp.

You will use .match() as a property on your string value that you are testing, and inside the parentheses, you will put your RegExp pattern. The return value will be an array of all the matches or an empty array. The returned array will be dependent on the g (or global) flag that returns all occurrences of the pattern you want to match. …


The pipe symbol and capture groups in regular expressions.

Introduction

Alternate characters in regular expressions can be very useful in addition to the many ways I have previously shown you in this series to match patterns. I’ll first start with the | pipe symbol which works very much the same way as it does in our conditional statements, functioning as ‘or’. Second, I’ll show you how to also use the () parenthesis in the regular expression to create capture groups as well to help specify which character we want to only be looking at first.

Again, if you want to follow along, I will be using the free site regex101.com.

Pipe Symbol

The pipe symbol in a regular expression allows you to use a conditional to search for the pattern you want by way of the ‘or’ statement. …


Introduction

One thing I like using regular expression for is when I need to match a pattern that is at the beginning or end of a string. In this post, I will quickly show you how to do that!

Matching the Beginning of A String

To start with matching the start of the string, we will use the ^ symbol. You can use either the character sets as I have shown previously or literal character matches. You will put the carat symbol before the pattern you are trying to match like this:

/^<insertPatternHere>/(optional flags here)/

Image for post
Image for post

I have chosen the literal string “cat.” I want to match any string that begins with the literal characters of “cat”. As you can see on the first line, it matches the first string, but on the second line, it does not match because that string starts with the string “dog”. Finally, our last string is matched because that one does start with “cat”. …


Wildcard, Zero or More Repetition & Optional Character

Introduction

This is the final installment of my special characters post within my regular expressions series. I will mention in this post, the wildcard character, optional character, and the zero or more repetitions character.

Let’s start with the optional character as that one is my favorite. I will be using regex101.com again to create the examples. If you want to follow along, you can use that website as it is an excellent tool for testing your regex.

Optional Character

The optional character uses the ? in the regular expression. If you want to match a pure question mark for punctuation purposes, you will have to escape it like all special characters using the backslash\?


Introduction

I promised that in this blog post that I would introduce how to use quantifiers with special and meta characters. The great thing about quantifiers is that you can use them to tell how many times a character should repeat. For example, when I introduced character sets, we might try to match a pattern such as

/[a-z]11/gi

This would mean that only the first character would be an alphabetical character of ‘a’ through ‘z’ and the next characters would be ‘1’ and ‘1’. However, what if we wanted to match a string where the first four characters were alphabetical followed by our two 1's? We might have to write it out in multiple character sets or we can specify with quantifiers. …


special & meta characters

Introduction

I have introduced ranges in this post which is a great and efficient way to go about writing out for example matches that are within a span of consecutive characters rather than having to manually write each one of them out. In this post, I will be mentioning another great feature of regex that are called metacharacters.

Metacharacters in regular expressions help match characters based on what catergories they may fit into. For example, \d will match any digit character that is (0–9). You can use metacharacters like \d in your regex like:

make sure to have the backslash when using the metacharacters. …


Ranges

Introduction

In my last blog post, I showed how you could use character sets in order to find matches. In this blog post, I am going to build off of that and introduce to you the effectiveness of ranges. Ranges are a great thing in regular expressions, as you will see when we are looking at a consecutive group of letters or numbers for example. Let’s get into the post!

We will be using my favorite resource for regular expressions regex101.com to display the work.

Code

Letters

We will use the test string ‘Lets test this string.’

Image for post
Image for post

In order to use your range, you will have to put it inside of the character set just as I showed in the last post. …

About

Reina Mitchell

Software Developer && Political Enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store