Metropolitan State University

ICS 225 Web Design and Implementation

 

Lab 4:  Strings and Regular Expressions

 

Objectives:   In this lab, you will work with JavaScript

  • regular expressions

 

Part I.  Practice with Regular Expressions

 

  1. Practice reading and understanding regular expression by playing with this test program:  http://cs.metrostate.edu/~fitzgesu/courses/ics225/fall2009/examples/regexp/regexp.html  Choose strings to match the patterns given in the example.

 

  1. Use View | Source to look at the code.  Find this section:

 

      function checkpattern ( pat, str )  {

            if ( str.match(pat) )         // if result is not null

                  alert ("Matched!");

            else

                  alert ("No match");

      }

 

  • Would the pattern matching function, search() work as well as the function match() here?  If so, what changes would need to be made to the code?
  • What about the pattern matching function, test()?

 

 

Part II.  Practice Some More with Regular Expressions

 

McFarland has provided a regular expression sandbox which I have posted at: http://cs.metrostate.edu/~fitzgesu/courses/ics225/fall2009/examples/regexp/regex_tester.html

 

Play with this regular expression tester.  At a minimum, create and test regular expressions for

  • 5 digit zip code
  • 9 digit zip code
  • either 5 digit or 9 digit zip code
  • U.S. phone number
  • phone number with spaces or dashes or periods separating the components
  • email address
  • date with 2 digit year
  • date with 4 digit year
  • date with elements separated by dashes or slashes
  • web address

 

Hint:  see McFarland, pages 126-130