This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Javascript Regular Expressions: Find Plain Text Patterns

2:29 JavaScript lesson by

The simplest use of Regular Expressions is to find a plain text pattern. In this lesson we'll look at at finding plain text patterns as well as using the metacharacter "." and how to escape a metacharacter.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

The simplest use of Regular Expressions is to find a plain text pattern. In this lesson we'll look at at finding plain text patterns as well as using the metacharacter "." and how to escape a metacharacter.

Avatar
Kode

I think this author doesn't know what a non-breaking space is. A space is unicode character 0x20 and a non-breaking space is 0xA0.

In reply to egghead.io

Finding a plain text pattern with regular expressions is fairly trivial. So we're going to have a string here called, or it's going to read "Cat sat on the hat." If we wanted to find the pattern C-A-T we can just set up a regular expression to cat, save that, and we can see that we are in fact getting cat. If we wanted hat, we just change that there, if we wanted sat we save that there. Now if we wanted to find all of the lower case As followed by lower case t, we just use our global flag and we get the three lower case As follows by lower case Ts in our string.

Now if we wanted to find all of the strings that are a character followed by at, we can use the metacharacter dot. Now you can see we've identified the three patterns that our regular expression represents, so we've got cat, sat, and hat. Now the dot metacharacter identifies any character including letters, digits, and a dash, but not a line break. So if I save that, you can see we've got a pattern on each and every one of these guys, including the non-breaking spaces.

If I go up here and add some line breaks to our string, everything is going to work just fine. But what we can do is say we're looking for any character, followed by any character, followed by any character. If I save that, we get our cat, we get sat, for the O-N we get our non-breaking space and on, the we get T-H-E, and then after that we get a non-breaking space followed by H-A, so that's one pattern, then we've got a t and a dot, and it doesn't have a third character, so that is not a match.

Also, if we add a fourth one here, cat no longer matches because the character after the t is a line break. Then if we go ahead and add a few more of those you can see that we get down to the last line which is the only line that's long enough to match our current pattern.

Now if we wanted to match a literal dot in our string we need to escape it since the dot is a metacharacter in regular expressions. So to do that we just add backslash right before the metacharacter that we want to match as a literal character, and then you can see that we in fact match the dot in our string.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?