Check out a free preview of the full JavaScript: The Recent Parts course:
The "Regex Exercise" Lesson is part of the full, JavaScript: The Recent Parts course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to utilize all three of the features learned in the last few sections, as well as earlier lessons to log selected portions of a poem provided by Kyle.

Get Unlimited Access Now

Transcript from the "Regex Exercise" Lesson

>> Kyle Simpson: All right, so we've talked about a variety of improvements that recently landed with regular expressions. So it seems like a great idea for us to try an exercise, and practice with each of those three. In fact, we're gonna go ahead and pull in a feature from one of our previous exercises and previous units of discussion.

[00:00:17] We're also gonna use generators. So you can get more practice with generators. All right, so here's the exercise that you're gonna be doing. I've given you a poem here that's split out into these separate lines. This is The Power of a Smile by Tupac Shakur. I've given you this poem, and what you're going to do with this for loop that you see here.

[00:00:38] This for loop should be printing out the following messages. It should be printing out, if you notice the form of this poem is that there's a thing, and then the thing that it can do, and then another thing, and the thing that it can do. So you're going to be using a regular expression to parse those tuples of values out of this poem, and return those as values from this iterable generator.

[00:01:07] So in other words, you're going to make a powers generator that is going to use a regular expression to iterate over this, and every time it finds a match, it's going to send that out as a string, so that this console.log is printing those messages out. At the bottom, there's several hints that will be very useful to you.

[00:01:28] Number one, you're going to be setting up powers as a generator, which refer back to the iterators and generators exercise, if you need some refresher on that. Your iterator, your generator is going to be looping, but the loop that it's going to be doing is essentially a while loop that's executing a global regular expression.

[00:01:49] So I've given you this pattern here, that if you have a regular expression that you define, which, that will be up to you. If you call .exec on it with a string, it will give you the next match, and then it will keep giving you the next match, and the next match, until it doesn't find anymore matches in the string.

[00:02:08] So it's a way to step through the matches one at a time, and that's a feature of global flagged regular expressions. You'll also notice here that you're going to need the .all modes, so I've already solved part of it for you, in the regular expression. You're gonna use the .all mode to be able to make those matches across the lines.

[00:02:28] There are a couple of, there is at least one place where the match has to span across a line, where we have the power, and then we have heal on a second line. Or I'm sorry, the smile and then heal on the next line. So your objective here is to use all three of the regular expression features that we just talked about.

[00:02:49] Recall that we have look behind assertions, both positive and negative. So if you're matching something like a noun, you need to make some sort of assertion that it comes immediately after something, for example. Like, for example, the word power of, the phrase power of. So you're gonna need to use some look behind assertions to make sure that your regular expression is finding the right word.

[00:03:15] And then you're also gonna be capturing some values out, and you wanna use named capture groups so that it's easier to tell which thing you're pulling out of the match. And then you're using, again, the .all mode so that you can make those matches across those new lines.

[00:03:30] So that is your exercise. It will be a little bit challenging, especially if you haven't done as much with regular expressions. If you need to get a little bit of a hint, feel free to take a look at the solution file, the ex.fixed file that's included. We will break for probably about ten minutes on this one, since it's a little more challenging to wrap your head around, and then we'll come back and talk about