Regular Expression Quantifiers allow us to identify a repeating sequence of characters of minimum and maximum lengths. In this lesson we'll use Regular Expression Quantifiers to match repeated patterns, common Quantifier patterns, and using shorthand for those common Quantifier patterns.
When we want to find a repeating value or character in our string with regular expressions, we can use something called a quantifier. So to illustrate that I'm creating a string here with seven As, seven lower case As in a row, and if we wanted to find let's say four As we could do one, two, three, four, I'll go ahead and throw in a global for now, and you can see that we do in fact our literal string text pattern of four As.
We can shorten that with a quantifier which we identify in curly brackets, and then we just tell it the number of characters that we're looking for. So if I save that, we get the exact same match. If I want to change it to five we'll get wherever five As in a row occur. Now we can add a comma here, and what this means is at least whatever the value is to the left, so in our case the five, so we're saying at least five on to infinity.
So if I save that, it's going to highlight the entire string, and if I add a max value after that comma, it will find a pattern where we have at least five As up to six, so we can see that six in a row are in fact highlighted. Now we also get some special patterns here, and the first one we'll take a look at is zero to infinity, so if I save that, everything's going to work just fine but we can also see is that we're actually getting a match on an empty string.
So if I change this to a B which we know doesn't exist, it's matching all of the empty strings, so zero will actually match an empty string. Now there's a shortcut for this particular pattern, and that's an asterisk. So we'll get the exact same results there, and there. Now another pattern is one comma, which is going to match at least one instance of our character on to infinity, and we get exactly what we'd expect, and there's a shortcut for this pattern as well, which is simply a plus symbol, so that is the equivalent of one to infinity.
Finally we'll take one more pattern here which is 0,1 which means zero or one instance of the character that we're expecting, so we get all of our As, as well as our empty string. The shortcut for this is a question mark, which really just ends up meaning that it's optional. So let's see if we can use these a bit more practically. I'm going to go ahead and drop in http://egghead.io on the next line, let's say I'm going to have https here, do a www on that guy, let's say not a Web address here, and on this one we'll just have the http://.
So let's start with matching either the http or the https. So the http or s and we want to make that optional, so we'll use 0 to 1, so let's try that out really quick, cool, all those are matching. That's going to be followed by a colon, and then we're going to escape two forward slashes, try that, cool. We've got all those. Now we want to match any character, let's start with zero to infinity, so any number of times, and that works pretty well.
However, we are getting this guy, we don't want to match that because it's not a full address, so we're going to make this one to infinity. Now we've got our two Web addresses properly matched. Now we can shorten this guy up to the question mark metacharacter, and this, the one to infinity is the + symbol, so we'll save that and everything's working. Now one problem with this type of greedy quantifier on our + symbol here, is that it's going to match one to infinity.
So here we have a part that's clearly not part of the Web address, it is in fact going to match that. So we'll talk about making that guy lazy as opposed to greedy in future lessons.