Main Page

Complex Patterns

Complex Patterns
Regular expressions can represent simple patterns, as discussed in the previous sections, or they can rep-
resent complex patterns. Complex patterns are made up of more than just character classes and quanti-
fiers: They are made up of groups, backreferences, lookaheads, and other powerful regular expression
functions. This section introduces these concepts and more, so you can use you regular expressions to
make complex string manipulations easier.
So far in this chapter, you’ve learned how to deal with regular expressions on a character-by-character
basis. As you might expect, certain character sequences, instead of containing just individual characters,
repeat themselves. To handle character sequences, regular expressions support grouping.
Grouping is used by enclosing a set of characters, character classes, and/or quantifiers inside of a set of
parentheses. For instance, suppose you wanted to match the string
. Using the knowledge
gained up to this point, you might predict the expression would probably look like this:
var reDogDog = /dogdog/g;
Although this is fine, it’s a bit wasteful. What if you don’t know how many occurrences of
will be in
the string? You can rewrite this expression using grouping such as the following:
var reDogDog = /(dog){2}/g;
The parentheses in this expression say that the sequence
will occur twice in a row. But you’re not
limited to using curly braces with groups; you can use any and all quantifiers:
var re1 = /(dog)?/; //match zero or one occurrences of “dog”
var re2 = /(dog)*/; //match zero or more occurrences of “dog”
var re3 = /(dog)+/; //match one or more occurrences of “dog”
You can even make some pretty complicated groups using a mixture of character literals, character
classes, and quantifiers:
var re = /([bd]ad?)*/; //match zero or more occurrences of “ba”, “da”, “bad”, or
And don’t be afraid to put groups inside of groups:
var re = /(mom( and dad)?)/; //match “mom” or “mom and dad”
This expression says that the string
is required, but the entire string
“ and dad”
can be there
zero or one times. Groups can also be used to make up for language features that JavaScript lacks.
For most programming languages with strings, a method to trim leading and trailing white space is a stan-
dard offering. JavaScript, however, has been without such a method since its introduction. Fortunately, reg-
ular expressions (with the help of groups) make it easy to create a
method for strings.
Regular Expressions
10_579088 ch07.qxd 3/28/05 11:38 AM Page 205

JavaScript EditorFree JavaScript Editor     Ajax Editor