Java Reference
In-Depth Information
<title>Chapter 6, Figure 2</title>
</head>
<body>
<script>
var myString = "Paul, Paula, Pauline, paul, Paul";
var myRegExp = /Paul/gi;
 
myString = myString.replace(myRegExp, "Ringo");
alert(myString);
</script>
</body>
</html>
figure 6-5
You used this code to convert all instances of Paul or paul to Ringo .
However, you found that this code actually converts all instances of Paul to Ringo , even when the
word Paul is inside another word.
One way to solve this problem would be to replace the string Paul only where it is followed by a
non‐word character. The special character for non‐word characters is \W , so you need to alter the
regular expression to the following:
var myRegExp = /Paul\W/gi;
This gives the result shown in Figure 6-6.
Search WWH ::




Custom Search