HTML and CSS Reference
In-Depth Information
EXPLANATION
1
The variable called
myString
is assigned a string of lowercase letters, just exactly
like the last example.
2
The regular expression reads: Search for one or more lowercase letters, but after
the + sign, there is a question mark. The question mark turns off the greed factor.
Now instead of taking as many lowercase letters as it can, this regular expression
search stops after it finds the first lowercase character, and then replaces that char-
acter with
XXX
. See Figure 17.27.
Figure 17.27
This is not greedy: Output from Example 17.25.
Often it is necessary to anchor a metacharacter down, so that it matches only if the pat-
tern is found at the beginning or end of a line, word, or string. These metacharacters are
based on a position just to the left or to the right of the character that is being matched.
Anchors are technically called
zero-width assertions
because they correspond to posi-
tions, not actual characters in a string; for example,
/^abc/
will search for
abc
at the
beginning of the line, where the ^ represents a position, not an actual character. See
Table 17.10 for a list of anchoring metacharacters.
Table 17.10
Anchors (Assertions)
Metacharacter
What It Matches
^
Matches to beginning of line or beginning of a string.
$
Matches to end of line or end of a string.
\b
Matches a word boundary (when not inside
[ ]
).
\B
Matches a nonword boundary.
EXAMPLE
17.26
<html>
<head><title></title></head>
<body>