Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson

Already subscribed? Sign In

Autoplay

    Find Plain Text Patterns using Regular Expressions

    Joe MaddaloneJoe Maddalone

    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.

    javascriptJavaScript
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    00:00 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.

    00:41 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.

    01:15 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.

    01:47 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.

    02:05 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.

    Discuss

    Discuss