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);
contains four items:
and are followed by a character in the range
1 through 4
is a character class that is made up of several other character classes. For instance,
suppose you want to match all letters
, and the new line character.
The class looks like this:
Note that there are no spaces between the different internal 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 white-space character
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
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
10_579088 ch07.qxd 3/28/05 11:38 AM Page 200