Main Page

Combination classes

Range classes work whenever the characters you want to test are in order by character code. Consider
the following example:
var sToMatch = “num1, num2, num3, num4, num5, num6, num7, num8, num9”;
var reOneToFour = /num[1-4]/gi;
var arrMatches = sToMatch.match(reOneToFour);
After execution,
arrMatches
contains four items:
“num1”
,
“num2”
,
“num3”
, and
“num4”
because they
all match
num
and are followed by a character in the range
1 through 4
.
Combination classes
A
combination class
is a character class that is made up of several other character classes. For instance,
suppose you want to match all letters
a
through
m
, numbers
1
through
4
, and the new line character.
The class looks like this:
[a-m1-4\n]
Note that there are no spaces between the different internal classes.
Predefined classes
Because some patterns are used over and over again, a set of predefined character classes is used to
make it easy for you to specify some complex classes. The following table lists all the predefined classes:
Code Equal To
Matches
.
[^\n\r]
Any character except new line and carriage return
\d
[0-9]
A digit
\D
[^0-9]
A non-digit
\s
[ \t\n\x0B\f\r]
A white-space character
JavaScript/ECMAScript doesn’t support union and intersection classes as do other
regular expression implementations. This means you can’t make patterns such as
[a-m[p-z]]
or
[a-m[^b-e]]
.
You can also negate range classes so as to exclude all characters within a given range.
For example, to exclude characters 1 through 4, the class is
[^1-4]
.
Note that
[a-z]
matches only lowercase letters unless the regular expression is set
to case insensitive by using the i option. To match only uppercase letters, you must
use
[A-Z]
.
200
Chapter 7
10_579088 ch07.qxd 3/28/05 11:38 AM Page 200


JavaScript EditorFree JavaScript Editor     Ajax Editor


©