HTML and CSS Reference
In-Depth Information
Figure H-1
Character positions
Regular
Expression
Character
Description
Text String
Match?
^
start of text string
/^audio/
audio studio
yes
great audio
no
$
end of text string
/audio$/
audio studio
no
great audio
yes
\b
word boundary
/audio\b/
audio studio
yes
audiophile
no
\B
no word boundary
/audio\B/
audio studio
no
audiophile
yes
Regular expressions recognize the beginning and end of a text string, indicated by the
^
and
$
characters, respectively. The following pattern uses the
^
character to mark the
start of the text string:
/^audio/
Any text string that starts with the characters
audio
is matched by this regular expression;
however, a text string such as
great audio
, in which the characters
audio
are not at the
start of the text string, would not be matched. In the same way, the end of the text string
is indicated by the
$
character and thus, the following regular expression matches only
text strings that end with
audio
:
/audio$/
The
^
and
$
characters are often used together to define a pattern for a complete text string.
For example, the following pattern matches only the text string
audio
and nothing else:
/^audio$/
The other positioning characters are used to locate words within a text string. The term
word
has a special meaning in regular expressions.
Words
are composed of word char-
acters, where a
word character
is any letter or number, or the underscore. Symbols such
as *, &, and - are not considered word characters, nor are spaces, periods, or tabs. Thus
R2D2
is considered a single word, but
R2D2&C3PO
is considered two words, with the
&
symbol acting as a boundary between the words. In a regular expression, the presence of
a word boundary is indicated with the
\b
symbol. Thus, the following pattern matches
any word that starts with the characters
art
, but does not match words that do not start
with these characters, such as
dart
:
/\bart/
The
\b
symbol can also indicate a word boundary at the end of a word. Thus, the follow-
ing regular expression matches words such as
dart
and
heart
, but not
artist
:
/art\b/
If you place the
\b
symbol at both the beginning and the end of a pattern, you define
a complete word. Thus, the following expression matches only the word
art
but no
other word:
/\bart\b/