Java Reference
In-Depth Information
Save the page as ch6 _ example4.html . Load the page into your browser and you should see what is
shown in Figure 6-9.
figure 6-9
You can see that by using regular expressions, you have completed a task in a couple of lines of simple
code. Without regular expressions, it would probably take four or five times that amount.
The workhorses of this code are two simple lines:
var myRegExp = /\B'|'\B/g;
text = text.replace(myRegExp, '"');
You define your regular expression (as discussed previously), which matches any non-word boundary
followed by a single quote or any single quote followed by a non-word boundary. For example, 'H will
match, as will H' , but O'R won't, because the quote is between two word boundaries. Don't forget that a
word boundary is the position between the start or end of a word and a non‐word character, such as a
space or punctuation mark.
The second line of code uses the replace() method to do the character pattern search and replace, and
assigns the new value to the text variable.
the search() method
The search() method enables you to search a string for a pattern of characters. If the pattern is
found, the character position at which it was found is returned; otherwise, ‐1 is returned. The
method takes only one parameter, the RegExp object you have created.
 
Search WWH ::




Custom Search