COM271, Week 9

Regular Expressions

Syllabus | Table of Pages | Assignments | References and Useful Links

A Regular Expression specifies a pattern for characters or numbers, against which the pattern of any variable (e.g., the input string from a form) can be compared. Regular expressions can be created with the RegExp ( ) constructor, providing a variety of useful methods. The first argument to RegExp is the desired pattern; the second is optional and contains any special flags for that expression, e.g.,
var pattern = new RegExp ("wxyz", "i");
They can also be created as literals, where characters of the pattern are surrounded by slashes ( / ), e.g.,
var pattern = /wxyz/;.
The pattern can be followed by flags which alter the interpretation, as

Test method: Once a pattern has been created, it will be recognized as a regular expression, whether it was created by the RegExp() constructor or as a literal. Then, you may use .test to compare a string to the pattern. In this example, we create a pattern and store the regular expression as the variable "pattern;" then we append the test method (.test) to the variable name, using it to obtain a boolean evaluation (which can take the values true or false) that tells us whether our pattern matches the expression we are passing it, here a string containing a web address:
var pattern = /WXYZ/;
pattern.test("http://www.wxyz.org/")
will return false (pattern only matches uppercase " WXYZ "); we can use this result to test for pattern matches by using it in an appropriate if condition (below).

Creating Patterns: RegExp uses character sequences to create patterns, e.g., to indicate that a character or set of characters should be repeated or should be excluded, etc. Let's look at a set of ways that these patterns are set, and then we'll use them in several examples, below:

Regular Expression Pattern Language

Common Character Classes: Frequently used character classes have shorthand escape codes

Alternatives (logical OR): The | indicates the logical OR of several items, used to separate complete patterns.

RegExp Object

The RegExp and String objects can be used to test and parse strings.

test ( ): This method returns true or false indicating whether a given string argument matches a regular expression. Use as:
var pattern new RegExp ("a*bbbc", "i");
bol_x = pattern.test ("1a12c")); //false
bol_y = pattern.test ("aaabBbcded")); //true

String Methods for Regular Expressions: The String object has four methods, intended to use regular expressions to modify of break up strings, in addition to matching and extracting:

JS & DOM Reference

Overview

JavaScript Objects

JS Array JS Boolean JS Date JS Math JS Number JS String JS RegExp JS Global

Browser Objects

Window Navigator Screen History Location

HTML DOM Objects

Document Events Elements Anchor Area Base Body Button Form Frame/IFrame Frameset Image Input Button Input Checkbox Input File Input Hidden Input Password Input Radio Input Reset Input Submit Input Text Link Meta Object Option Select Style Table TableCell TableRow Textarea