HTML and CSS Reference
In-Depth Information
Figure H-3
Character classes
Regular
Expression
Class
Description
Matches
[ chars ]
class based on the characters
listed in chars
/[tap]/
t, a, or p
[^ chars ]
class based on the characters
not listed in chars
/[tap]/
any character that is not
t, a, or p
[ char1-charN ]
/[a-m]/
class based on characters
from char1 through charN
any letter from a
through m
[^ char1-charN ] class based on character not
listed from char1 through
charN
/[^a-m]/
any character that is not
a through m
[a-z]
all lowercase letters
/[a-z][a-z]/ two consecutive lowercase
letters
[A-Z]
/[A-Z][A-Z]/ two consecutive
uppercase letters
all uppercase letters
[a-zA-Z]
all letters
/[a-zA-Z]
[a-zA-Z]/
two consecutive letters
[0-9]
all digits
/[1][0-9]/
the numbers 10
through 19
[0-9a-zA-Z]
all digits and letters
/[0-9a-zA-Z]
[0-9a-zA-Z]/
any two consecutive
letters or numbers
Specifying Character Repetition
The regular expression symbols you've seen so far have all applied to single characters.
Regular expressions can also include symbols that indicate the number of times to repeat
a particular character. To specify the exact number of times to repeat a character, you
append the character with the symbol
{ n }
where n is the number of times to repeat the character. For example, to specify that a text
string should contain only five digits—as in a postal code—you could use either
/^\d\d\d\d\d$/
or the more compact form
/^\d{5}$/
For more general repetitions, you can use the symbol * for 0 or more repetitions, + for
1 or more repetitions, or ? for 0 repetitions or 1 repetition. Figure H-4 describes these
and other repetition characters supported by regular expressions.
 
Search WWH ::




Custom Search