HTML and CSS Reference
In-Depth Information
In some cases, you want to match substrings only within words. In these situations,
you use the \B symbol, which indicates the absence of a word boundary. The following
regular expression matches the substring art only when it occurs inside of a word such as
hearts or darts , but not next to a word boundary, as in artist or kart :
/\Bart\B/
Regular expression
symbols have opposite
meanings when expressed
in uppercase letters: \B
means the opposite of \b
and \w means the oppo-
site of \W .
Defining Character Types and Character Classes
Another class of regular expression characters indicates the type of character. The three
general types of characters are: word characters, digits (numbers 0 to 9), and white space
characters (blank spaces, tabs, and new lines). Figure H-2 describes the regular expres-
sion symbols for these character types.
Figure H-2
Character types
Regular
Expression
Character
Description
Text String
Match?
\d
a digit (from 0 to 9)
/\dth/
5th
yes
ath
no
\D
a non-digit
/\Dth/
5th
no
ath
yes
\w
a word character (an upper-
or lowercase letter, a digit,
or an underscore)
/\w\w/
A1
A$
yes
no
\W
a non-word character
/\W\W/
B3
no
$@
yes
\s
a white space character (a
blank space, tab, new line,
carriage return, or form feed)
/\d\s\s\d/
5 3
53
yes
no
\S
a non-white-space character
/\d\S\S\d/
5AB3
yes
5 3
no
.
any character
/.../
abc
yes
a c
yes
For example, a digit is represented by the \d symbol. To match any occurrence of a
single digit, you use the regular expression
/\d/
This expression would find matches in such text strings as 105 , 6 , or U2 , because they all
contain an instance of at least one single digit. If you want to match several consecutive
digits, you can simply repeat the \d symbol. The following regular expression matches
3 consecutive digits:
/\d\d\d/
and thus would match text strings such as 105 or EX1250 , but not 27 or E2 . If you
wanted to limit matches only to words consisting of three-digit numbers, you could use
 
Search WWH ::




Custom Search