Better Programming

Advice for programmers.

Follow publication

Member-only story

Breaking Down a Complex Regex

Annie Liao
Better Programming
Published in
3 min readJan 18, 2020

--

Photo by the author.

While learning multiple ways to create a Pig Latinizer, I struggled to understand how a complex regex (regular expression) works its magic inside a .split method.

More specifically, I was amazed yet perplexed by a simple line of code (via this amazing programmer):

"forest".split(/([aeiou].*)/)
# => ["f", "orest"]

The goal of this split method is to divide a word into an array of two strings, with the first vowel of the word as a delimiter. As illustrated above, the first string contains all character(s) before the first vowel, and the second string has all characters after the first vowel (including the vowel itself).

To demystify the complexity of this split/regex combo, I decided to, uh, “split” up the regex — one regular expression at a time.

Regex 1: /[aeiou]/

[] is a character class that allows us to find any matching characters in the string. The character(s) in the character class acts as a delimiter. When a match is found, the characters in the string are divided/split.

Here, we can see the word forest being split by o and e, returning an array of divided strings:

"forest".split(/[aeiou]/)
# dividers => ("o")("e")
# returns =>…

--

--

Annie Liao
Annie Liao

Written by Annie Liao

Fullstack Developer: React with Rails. Currently exploring data structures, D3 visualization, and other front-end magic.

Write a response