Java Reference
In-Depth Information
You can also use special characters to specify pattern or character repetition. Additionally,
you can specify what the pattern boundaries must be—for example, at the beginning or end
of the string, or next to a word or non-word boundary.
Finally, you can define groups of characters that can be used later in the regular expression or
in the results of using the expression with the replace() method.
In the next chapter, you take a look at using and manipulating dates and times using JavaScript,
and time conversion between different world time zones. Also covered is how to create a timer that
executes code at regular intervals after the page is loaded.
exerCises
You can find suggested solutions to these questions in Appendix A.
1. What problem does the following code solve?
var myString = "This sentence has has a fault and and we need to fix it."
var myRegExp = /(\b\w+\b) \1/g;
myString = myString.replace(myRegExp,”$1”);
Now imagine that you change that code, so that you create the RegExp object like this:
var myRegExp = new RegExp("(\b\w+\b) \1");
Why would this not work, and how could you rectify the problem?
2. Write a regular expression that finds all of the occurrences of the word “a” in the following
sentence and replaces them with “the”:
“a dog walked in off a street and ordered a finest beer”
The sentence should become:
“the dog walked in off the street and ordered the finest beer”
3. Imagine you have a website with a message board. Write a regular expression that would
remove barred words. (You can make up your own words!)
 
Search WWH ::




Custom Search