Next page | Contents page |

Regular Expressions

Regular expressions provide a powerful way of matching patterns in text. They are used in the grep command in Unix and in various programming languages, including Perl, Java and JavaScript.

They provide useful ways of validating user input text which, as we have already been warned, is vitally important for web site security.

The syntax is not trivial and really requires another course in itself. We will simply give some examples.

In JavaScript we can define patterns (objects of type RegExp) by slash delimiters. Eg,


  var pattern1 = /JavaScript/;        
      // Only matches those exact letters with cases as given
	  
  var pattern2 = /[Jj]ava[Ss]cript/;  
      // Matches with upper or lower case J or S
	  
  var pattern3 = /J[A-Z]{9}/;         
      // Capital J followed by any 9 capital letters
	  
  var pattern4 = /J[A-Za-z]{6,9}/;    
      // Capital J followed by from 6 to 9 letters of either case

  // To specify a string comprising an integer only:
  var pattern5 = /^(\+|-)?[0-9]+$/;  
  // ^ and $ mean at start and end of string.
  // (|) for listing alternatives.
  // ? means 0 or 1 occurrences: the number's optional sign.
  // [0-9] means any digit (only).
  // + means 1 or more occurrences. Must be at least one digit.
  // + has special meaning so must write \+ to use as a character.

You could alternatively use a long-hand version involving a constructor:


  var pattern4 = new RegExp ("J[A-Za-z]{6,9}");

There are then some String methods using RegExp objects as parameters:


  var a = s.search (regexp); // Returns index of match (or -1 if not found)
  var b = s.match (regexp);  // Returns array of all matching Strings
  var c = s.replace (regexp, "new text");

RegExp also has methods. Returned objects have several useful properties.

The Mozilla site has a useful guide to regular expressions.

Next page | Contents page |