JavaScript Lesson 11, Regular Expressions, Homework Questions

Q1

Write a program which prompts the user to enter text into a text area, then searches that text for all four-letter words, and replaces them with ****. It should only alter four-letter words, not words with more than four letters.

Q2

Assuming that the string s has been set to Had we had haddock for tea, we would have had enough! what is returned by each of the following method calls?

s.match(/had/); s.match(/had\b/); s.replace(/\swe\s/,"x");
s.match(/had/g); s.match(/had\b/g); s.replace(/\shad/,"x");
s.match(/had/i); s.replace(/had/g,"Did"); s.match(/\shad\s/);
s.match(/had/gi); s.replace(/\$had/i,"Did"); s.match(/\bhad\b/);

Q3

If we want to find the word computer inside a text string, it wouldn’t be a good idea to use the regular expression /\scomputer\s/ Why not? How should the regular expression be rewritten to make it fool-proof? What should the regular expression be if you wanted it to match the words computer and computers but no other word?

Q4

In Slobovia, all the telephone numbers take the form of a 0 followed by a # symbol, followed by 3 digits, another # symbol and another four digits. All car licence plates take the form of a letter of the alphabet (which can be upper or lower case), three digits, then a single space followed by three more letters of the alphabet (again, upper or lower case). Write two regular expressions, one for matching Slobovian telephone numbers, the other for matching Slobovian car licence plate numbers. The regular expressions should only match substrings which are correctly formulated according to the rules above.

Q5

Each of the following regular expressions can be written in a more efficient way. How could they be rewritten?

/[abcde]/ /[^0427691035]/ /[^\w]/
/[0123456789]/ /\w\w\w\w\w\w\w\w\w/ /[^\D]/

Q6

Some web browsers (very old ones) cannot cope with JavaScript, so all JavaScript instructions between <SCRIPT> (or <SCRIPT LANGUAGE="JavaScript">) and </SCRIPT> tags must be removed, including the <SCRIPT> and </SCRIPT> tags themselves. Write a program which takes the contents of a text area, which we may assume to be the HTML for a web page, and removes all the JavaScript code as specified. You needn’t worry about how the code gets into the text area - it could always be placed there by the user, via Copy and Paste.

Q7

The regular expression /\$\$/ only matches one thing. What is that thing?

Q8

Some web browsers cannot cope with colour names in tags such as <font>, preferring hexadecimal character codes, such as #ff0000 (for red), #00ff00 (for green) and #0000ff (for blue), for example. Write a program similar to the one in question 6 that searches for such tags and replaces the colour name with the hexadecimal code. Remember, the patterns that you are looking for are basically color=red (where red, of course, can be any colour name), except that there may be any number of spaces or new line characters both before and after the equals sign. You might like to adapt the program so that it can pick up other references to colours, such as link=blue (in the <body> tag) or bgcolor=yellow (in the <body> tag or as a background for table divisions).

Q9

Is it possible to use a regular expression to split a string into an array elements, i.e. is it possible to pass a regular expression as the parameter to the split() method of a string?