Here is the video version for this topic. You can read the written version which comes after the video section.

Previously, we looked at character classes which allows you to specify a "this or that". Another similar concept in regular expressions is alternation, also called alternating characters.

Alternation allows you to specify a "this" or "that" character or group of characters. You create them with the pipe | symbol. For example:

/this|that/g

This pattern will match the characters "this" or "that":

Expand
Regex/this|that/g
InputI love this but I do not love that
Match

Because we have the g flag , this pattern matches "this" and "that".

How does this differ from character classes?

The difference is:

Important

Character classes only allows you to provide characters: [abc]. It does not allow you to provide other expressions like groups and quantifiers.

For example, let's say you have a character class of the \w meta character, and the + plus quantifier which means 1 OR MORE:

/[\w+]/

Passing plus + in a character class will treat it as a “normal character” and not a quantifier. Every special character in a character class is treated as a normal character.

Expand
Regex/[\w+]/g
Inputhe likes to ping $&6...what is +ing
Match

As you can see above, every character except "$" and "&" are not matched. These are the only characters that are neither \w (word characters) nor + in the character class.

Also if you attempt to have groups in a character class, like this:

/[(ha)]ing/

It also won't work, as the parentheses would be treated as normal bracket characters. So, this character class would mean “open parentheses (” or “h” or “a” or “close parentheses )”.

Expand
Regex/[(ha)]ing/g
Inputrandom characters: (ing, aing, )ing
Match

From the matches, you see "(ing", "aing", and ")ing", because they begin with a character from the character class followed by "ing".

This is how alternation is different.

Important

Unlike character classes, alternation allows you to combine other special characters in your "this or that" pattern.

With alternation, you can provide expressions like characters, groups, quantifiers or any other valid regular expression. For example:

/th(is|at)/

Here we have a group and in it, we use alternation to indicate "is" or "at". So the pattern here means:

"t" followed by "h" followed by "is" or "at":

Expand
Regex/th(is|at)/g
Inputhe said this, he didn't say that
Match

The orange colors signifies the groups that are captured. Remember you can turn off capturing with a question mark followed by a colon in the group: (?:..)

Let's see another example. Let's say we had a string like:

"I grabbed a refreshing can of Coca-Cola, or as some people call it, Coke, while others prefer to refer to it as Coca Cola"

And you want to match "Coca-Cola", "Coke" and "Coca Cola". Here are few ways you can use alternation for this:

Expand
Regex/Coca-Cola|Coke|Coca Cola/ig
InputI grabbed a refreshing can of Coca-Cola, or as some people call it, Coke, while others prefer to refer to it as Coca Cola
Match
The i flag to turn off case restriction.

Here we basically said "Coca-Cola" or "Coke" or "Coca Cola". Very easy to read, but we can make this shorter.


Expand
Regex/Co(ca-Cola|ke|ca Cola)/ig
InputI grabbed a refreshing can of Coca-Cola, or as some people call it, Coke, while others prefer to refer to it as Coca Cola
Match

Very slightly shorter, but what we're doing here is "Co" followed by:

  • "co-Cola" or
  • "ke" or
  • "ca Cola"

And of course, the groups are captured.

Now let's make this shorter.


Expand
Regex/Co(ca(-| )Cola|ke)/ig
InputI grabbed a refreshing can of Coca-Cola, or as some people call it, Coke, while others prefer to refer to it as Coca Cola
Match

Here we say, "Co" followed by:

  • "ca" followed by
    • "- or |"
    • followed by "Cola"
  • "ke"

Though shorter, this becomes complex, and readibility becomes difficult. Like I mentioned at the beginning of this course, there are many patterns you can write to achieve the same thing, but always keep reability in mind.

In this pattern, we have a nested group ca(-| ) which includes an alternation between "ca-" and "ca ". This way, we can match Coca-Cola and Coca Cola. While this does the job, I'd rather use our earlier solution:

/Coca-Cola|Coke|Coca Cola/ig

This is easier to read and pretty clear what we're trying to achieve.


Try to solve these exercises on your own, and you can share on Twitter and tag me @iamdillion.

To share your solution on Twitter, click on Edit. After entering your solution, click on the checkmark, then Copy Regex URL. Then, share.

Match all filenames with their extensions here.

Expand
Regex/your-regex/g
Inputi copied a lot of files: index.js, react-diagram.jpg,
my-resume.pdf, user111-profile.png, logo.jpeg, contacts.csv and background.png
Match


Match all domains in this string.

Expand
Regex/your-regex/g
InputThere are a lot of useful websites like example.com, freecodecamp.org, simple.code, and stackoverflow.net to gather information.
Match

Now let's move onto a more interesting concept known as Lookaheads.