Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
Finally, the course compares how code is written in several different languages, the libraries and frameworks that have grown around them, and the reasons to choose each one.
While strings have some great built-in functionality-- we can convert them to uppercase or lowercase, we can slice pieces out, we can find out if a word exists in the string-- well, sometimes we need a different approach and what we are more interested in is the format of the string. What I mean by that is maybe I want to ask, is it an email address? Not a particular email address, but does it match the pattern for one? Or is it a URL? Perhaps I want to ask if a password has a mixture of uppercase and lowercase letters and special symbols or verify that a credit card number has the correct amount of digits.
Now the word hello is actually as simple a pattern as you can get. What we are going to do is look for the word hello to exist in another string. So once we've created our regular expression, we then have to test it. So let's say I create a new variable called myString, and this is what I am going to test my regular expression against. So in my if statement, I'll ask if myRE.test, so .test is a built-in method. Again, it's like a function that belongs to this regular expression.
And I'm passing in the variable myString to say test this, check it, does it have the word hello in it. What's going to happen is we get a Boolean response from this. Calling test will return either true or false. In this case, if the word hello exists in myString, which it does, we are going to pop up an alert and say, yes. The regular expression objects have other methods that can return different data. More complex patterns are created by using special characters. So to give you a few examples, if I create a new regular expression variable here, and I'm using the shorthand format which uses the two forwards slashes, one at the start and one at the end, and whatever is between them is the pattern that we are matching on.
The actual forward slashes themselves are not part of the pattern. We've seen how to match on the word hello. Well, if I prefix this by this little character, which is the caret character, we are saying the hello has to be at the start. If instead my pattern was hello$ before the forward slash, it would have to be at the end. Using the plus inside the pattern means that the L would have to appear once or more. So it would match on these different examples. Hello with one l, with two ls, with multiple ls.
hel*o means that it would be a 0 or more. So it'd match all the previous ones, but it would also match heo. The l doesn't have to appear at all. We have the question mark for 0 or 1 match, which will be a bit more restrictive on what it was matching. It would match heo or helo, but would not match more than that. Using the pipe, the vertical bar symbol, means we are actually matching either hello or goodbye. The decimal point is the period, means any character.
Using a /w inside the pattern is matching an alphanumeric or and underscore. \b inside the pattern means we are looking for what's called a word boundary like a space or a new line. You can also use the square brackets to provide a range of characters to match on. In this case, what we are looking for is either crnl or d and then the ope, so it would match on cope and rope and lope and dope. As you're beginning to tell there is a lot of these and there's actually way more than I am showing here, and you can describe extremely complex patterns by stringing them all together.
Here is one for example that describes a US zip code with an optional four digit extension. I'm not going to go through this. Bear in mind, you're going to begin by finding and looking for examples online. If you're looking for regular expressions to match a date or a password or credit card format, there really is no reason that you should be trying to create it yourself from scratch. This next regular expression here is one that tries to match on an email address. In fact, matching an email address is a pretty complex operation and people have been debating for many years on what the perfect regular expression for an email address is.
It looks very complex, but you can actually see there's kind of a repetition of the same kind of thing going on through it. It's really because an email address can follow quite at a lot of complex rules. Email addresses are notoriously difficult to match on regular expressions and this one is certainly not perfect, but it's kind of an example of what you might see and what you might come across. You will find that sometimes with more complex patterns that you're not looking for a perfect match. You're just looking for one that's good enough. And it is worth keeping in mind with regular expressions, particularly if you're brand-new to them, that I wouldn't be at all focused on trying to memorize these formats.
There are currently no FAQs about Foundations of Programming: Fundamentals.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.