↑
Main Page
reluctant quantifier
Boundary
Description
^
Beginning of the line
$
End of the line
\b
Word boundary
\B
Non-word boundary
Suppose that you want to find a word, but only if it appears at the end of the line. You can use the dollar
sign (
$
) to indicate this:
var sToMatch = “Important word is the last one.”;
var reLastWord = /(\w+)\.$/;
reLastWord.test(sToMatch);
alert(RegExp.$1); //outputs “one”
The regular expression in this example looks for the last word with one or more word characters preced-
ing a period that appears before the end of the line. When this expression is run against
sToMatch
, it
returns
“one”
. You can easily change this expression to get the first word in the line by using the caret
(
^
) character:
var sToMatch = “Important word is the last one.”;
var reFirstWord = /^(\w+)/;
reFirstWord.test(sToMatch);
alert(RegExp.$1); //outputs “Important”
In this example, the regular expression looks for the beginning of the line followed by one or more word
characters. If a non-word character is encountered, the match stops, returning
“Important”
. This exam-
ple can be easily updated to use a word boundary instead:
var sToMatch = “Important word is the last one.”;
var reFirstWord = /^(.+?)\b/;
reFirstWord.test(sToMatch);
alert(RegExp.$1); //outputs “Important”
Here, the regular expression uses a reluctant quantifier to specify any character can appear one or more
times before a word boundary (if a greedy quantifier is used, the expression matches the entire string).
Using the word boundary is a great way to extract words from a string.
var sToMatch = “First second third fourth fifth sixth”
var reWords = /\b(\S+?)\b/g;
var arrWords = sToMatch.match(reWords);
The regular expression
reWords
uses both the word boundary (
\b
) and the non-white space class (
\S
)
to extract the words in a sentence. After execution, the
arrWords
array contains
“First”
,
“second”
,
“third”
,
“fourth”
,
“fifth”
, and
“sixth”
. Note that the beginning of the line and the end of the line,
normally represented by
^
and
$
, respectively, both count as word boundaries so
“First”
and
“sixth”
are included in the result. This is not the only way to get all the words in a sentence, however.
211
Regular Expressions
10_579088 ch07.qxd 3/28/05 11:38 AM Page 211
Free JavaScript Editor
Ajax Editor
©
→