Java Reference
In-Depth Information
Remember that the fi rst / and last / tell JavaScript that what is in between those characters is a regular
expression. JavaScript creates a RegExp object based on this regular expression.
As another example, what if you have the string Paul Paula Pauline, and you want to replace Paul
and Paula with George? To do this, you would need a regular expression that matches both Paul and
Paula.
Let's break this down. You know you want the characters Paul, so your regular expression starts as
Paul
Now you also want to match Paula, but if you make your expression Paula, this will exclude a match
on Paul. This is where the special character ? comes in. It enables you to specify that the previous char-
acter is optional — it must appear zero (not at all) or one time. So, the solution is
Paula?
which you'd declare as
var myRegExp = /Paula?/
Position Characters
The third group of special characters you'll look at are those that enable you to specify either where the
match should start or end or what will be on either side of the character pattern. For example, you might
want your pattern to exist at the start or end of a string or line, or you might want it to be between two
words. The following table lists some of the most common position characters and what they do.
Position Character
Description
^
The pattern must be at the start of the string, or if it's a multi-line string,
then at the beginning of a line. For multi-line text (a string that contains
carriage returns), you need to set the multi-line fl ag when defi ning the
regular expression using /myreg ex/m . Note that this is only applicable to
IE 5.5 and later and NN 6 and later.
$
The pattern must be at the end of the string, or if it's a multi-line string,
then at the end of a line. For multi-line text (a string that contains carriage
returns), you need to set the multi-line fl ag when defi ning the regular
expression using /myreg ex/m . Note that this is only applicable to IE 5.5
and later and NN 6 and later.
\b
This matches a word boundary, which is essentially the point between a
word character and a non-word character.
\B
This matches a position that's not a word boundary.
Search WWH ::




Custom Search