To do this we encapsulate the
characters in square braces /[mf]onkey/.
There are a number of shortcuts we can use to make building character classes easier.
The dash character can be used to specify a range from character to character. This
example matches 'aonkey' through 'zonkey': /[a-z]onkey/.
So far we have dealt with simple consecutively matched items, but we can use
quantifiers to duplicate a pattern. ???uantifiers attach themselves to the pattern
element directly to the left. If we wanted to match monkey, but with as many 'o's as
we want we can do this: /mo+nkey/. The plus character '+' means we must have one
to many 'o's.
Chapter 11
[ 321 ]
Quantifier Description Example
+
One to many.
Matches monkey through mo...onkey.
/mo+nkey/
*
Zero to many.
Matches mnkey through mo...onkey.
/mo*nkey/
?
Optional.
Matches mnkey and monkey.
/mo?nkey/
{x} or {x,}
x number.
Matches mooonkey.
/mo{3}nkey/
{x,y}
x number to y number.
Matches monkey through mooonkey.
/mo{1,3}nkey/
We can add to the usefulness of quantifiers by surrounding a block in a pattern with
parentheses.
Pages:
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441