Java Reference
In-Depth Information
If you change the RegExp object in the code to the following, a global case-insensitive match will be made.
var myRegExp = /Paul/gi;
Running the code now produces the result shown in Figure 9-4.
Figure 9-4
This looks as if it has all gone horribly wrong. The regular expression has matched the
Paul
substrings
at the start and the end of the string, and the penultimate
paul
, just as you wanted. However, the
Paul
substrings inside
Pauline
and
Paula
have also been replaced.
The
RegExp
object has done its job correctly. You asked for all patterns of the characters
Paul
to be
replaced and that's what you got. What you actually meant was for all occurrences of
Paul
, when it's
a single word and not part of another word, such as
Paula
, to be replaced. The key to making regular
expressions work is to defi ne exactly the pattern of characters you mean, so that only that pattern can
match and no other. So let's do that.
1.
You wa nt
paul
or
Paul
to be replaced.
2.
You don't want it replaced when it's actually part of another word, as in Pauline.
How do you specify this second condition? How do you know when the word is joined to other charac-
ters, rather than just joined to spaces or punctuation or the start or end of the string?
To see how you can achieve the desired result with regular expressions, you need to enlist the help of
regular expression special characters. You'll look at these in the next section, by the end of which you
should be able to solve the problem.
Try It Out Regular Expression Tester
Getting your regular expression syntax correct can take some thought and time, so in this exercise
you'll create a simple regular expression tester to make life easier.
Type the following code in to your text editor and save it as
ch9_examp2.htm
:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8” />
<title>Regular Expression Tester</title>
<style type=”text/css”>
body,td,th {
font-family: Arial, Helvetica, sans-serif;