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,
contains four items:
, and
because they
all match
and are followed by a character in the range
1 through 4
Combination classes
combination class
is a character class that is made up of several other character classes. For instance,
suppose you want to match all letters
, numbers
, and the new line character.
The class looks like this:
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
Any character except new line and carriage return
A digit
A non-digit
[ \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
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
Note that
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
Chapter 7
10_579088 ch07.qxd 3/28/05 11:38 AM Page 200

JavaScript EditorFree JavaScript Editor     Ajax Editor