In their simplest form a Regular Expression is simply a description of the pattern we are trying to match. This pattern is placed inside a pair of /'s, for example in the replacement example on the previous page, our Regular Expression could be :
replace(/2002/,"2003")
Regular Expression ShortcutsHowever, Regular Expressions offer us shortcuts to represent specific groupings of character, for example :
In addition to these groupings we can negate them by adding a preceeding ^ symbol so ^\d means anything that is not a digit. In some cases we may be looking to match a set number of occurances of a pattern, for example for the phone code used in the first example we need to match 5 digits, so we could use :
/\d\d\d\d\d/ But this can very quickly become complex for large or complex patterns, we may also not know exactly how many digits we need to match, for example to validate a percentage value we could have a value of 6, 17 or even 100 percent. This means we are looking for a pattern which has one, two or even three digits, so a straight collection of \d's won't work. Luckily Regular Expressions provide repetition operators that we can use to overcome this problem :
Thus our Regular Expression of percentages becomes :
/\d{1,3}/ Complex Pattern MatchingIf we are looking for a code as our pattern where the first three characters is a combination of the following letters a, b, c, x, y or z followed by three digits. The last section is doable as we can use \d{3}, but we can't use \w as we don't want a d or j in there. Regular Expressions also allows us to specify a custom range between square brackets - [ and ] so we can do the following :
/[a,b,c,x,y,z]{3}\d{3}/ We can use a range operator to simplify the previous pattern, so we could have used :/[a-c,x-z]{3}\d{3}/ There are a few other groupings which can be used to ensure we get the pattern we are looking for and only the pattern we are looking for, the common ones are :
So we can now use these to produce complex Regular Expressions, for example :
Subpatterns and AlternativesWhat happens if our pattern can begin with either XY or AB followed by three digits, or we want to match win or wind but not windows ?Well, in the first case we can use the OR operator to search for XY or AB, followed by a standard \d{3} to find the rest, in the second one we can use the ? operator on the d so that we match zero or one - although we could equally use {0,1}. We can also use brackets to seperate out the sections or our Regular Expression. Therefore our Regular Expressions are :
/^(([X][Y])|([A][B]))\d{3}$/ and/^win(d)?$/ |