Java Reference
In-Depth Information
For example, if you wanted to make sure your pattern was at the start of a line, you would type the
following:
^myPattern
This would match an occurrence of myPattern if it was at the beginning of a line.
To match the same pattern, but at the end of a line, you would type the following:
myPattern$
The word-boundary special characters \b and \B can cause confusion, because they do not match
characters but the positions between characters.
Imagine you had the string “Hello world!, let's look at boundaries said 007.” defi ned in the
code as follows:
var myString = “Hello world!, let's look at boundaries said 007.”;
To make the word boundaries (that is, the boundaries between the words) of this string stand out, let's
convert them to the | character.
var myRegExp = /\b/g;
myString = myString.replace(myRegExp, “|”);
alert(myString);
You've replaced all the word boundaries, \b, with a |, and your message box looks like the one in
Figure 9-8.
Figure 9-8
You can see that the position between any word character (letters, numbers, or the underscore char-
acter) and any non-word character is a word boundary. You'll also notice that the boundary between
the start or end of the string and a word character is considered to be a word boundary. The end of
this string is a full stop. So the boundary between the full stop and the end of the string is a non-word
boundary, and therefore no | has been inserted.
If you change the regular expression in the example, so that it replaces non-word boundaries as follows:
var myRegExp = /\B/g;
you get the result shown in Figure 9-9.
Search WWH ::




Custom Search