Last updated 2015 February 10

Regular expresions

Try the regular expression practice program.

Regular expression symbols as documented by developer.mozilla.org the developer of Firefox. Gives an explanation and examples for all the regular expression symbols, of which, you will be given on the final exam the subset of symbols to know and be familiar with.

Basic definition

To start you have an alphabet of characters.
For example the characters that you can type on a keyboard.

Putting a sequence of characters together you have a string.
For example "This is a string".

You can then have a set of strings; for example {"ax", "bx" "cx", "dx"}.

A regular expression is a string of symbols describing a set of strings over an alphabet of characters.
For example {"ax", "bx" "cx", "dx"} can be described by the regular expression [abcd]x
While {"xa", "xb" "xc", "xd"} can be described by the regular expression x[abcd]

Regular expression symbols

Regular expression symbols as documented by developer.mozilla.org the developer of Firefox. Gives an explanation and examples for all the regular expression symbols.

The following are symbols used in regular expressions.

Regular expressions may also include the following search modifiers.

Look at examples in developer.mozilla.org webpage on regular expressions.

Functions that use regular expressions

The following Javascript functions use regular expressions for a pattern; and they can also use a string.

Creating regular expressions

In Javascript a literal regular expression is denoted using the "/" character.

var theRegularExpression = /db+d/g;
var theString = "cdbbdbsbz";
var Result = theString.replace(theRegularExpression, "123");

An alternative is to use the regular expression constructor.

var theRegularExpression = new RegExp("db+d","g");
var theString = "cdbbdbsbz";
var Result = theString.replace(theRegularExpression, "123");

Use regular expressions to swap names. Note that in Javascript you have to use '\\' in the string to get a single '\' in the pattern.

var theRegularExpression = /(\w+) (\w+)/;
var theString = "Leila Anderson";
var Result = theString.replace(theRegularExpression, "$2, $1");

An alternative is to use the regular expression constructor.

var theRegularExpression = new RegExp("(\\w+) (\\w+)","");
var theString = "Leila Anderson";
var Result = theString.replace(theRegularExpression, "$2, $1");

The function escapeRegExp(string) – puts the escape character in front of every character that needs to be escaped in string to prevent it from being a special character in a regular expression

function escapeRegExp(string){
  return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}

Given a string of name-pairs, firstName lastName, with each pair separated by a semi-colon, and with arbitrary white space create a string of names with the first and last names swapped.

var names = "Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ; Chris Hand ";
var namePattern = /(\w+)\s+(\w+)/; // firstName whiteSpace lastName
var newNames = names.replace(namePattern, "$2, $1");

Given a string of name-pairs, firstName lastName, with each pair separated by a semi-colon, and with arbitrary white space create a list of names with the first and last names swapped.

var names = "Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ; Chris Hand ";
var splitPattern = /\s*;\s*/; // All white space before and after a ';'
var nameList = names.split(splitPattern);
var namePattern = /(\w+)\s+(\w+)/; // firstName whiteSpace lastName
var namesSwappedList = []; // Create an empty list
var index;
for (index = 0 ; index < nameList.length ; index += 1) {
  namesSwappedList[index] = nameList[index].replace(namePattern, "$2, $1");
}

An alternate way of doing the previous problem.

var names = "Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ; Chris Hand ";
var namePattern = /(\w+)\s+(\w+)/g; // firstName whiteSpace lastName
var newNames = names.replace(namePattern, "$2, $1");
var splitPattern = /\s*;\s*/; // All white space before and after a ';'
var namesSwappedList = newNames.split(splitPattern);

A phone number consists of a three-digit area code followed by a three-digit exchange followed by a four-digit local number.

Variations are to permit the area code to be with parenthesis, e.g. (905), and to permit any of the characters ' '(space), '.' or '/' to separate the three parts, e.g. 123 456 9876, but the same character must be used in both places.

var phoneNumberPattern = /(?:\d{3}|\(\d{3}\))([\-\.\s])\d{3}\1\d{4}/;
An example web page is here.

it is important to document complex regular expressions.

  1. (?:\d{3}) matches 3 digits, does not remember the match
  2. \(\d{3}\) matches "( 3 digits )"
  3. (?:\d{3}|\(\d{3}\)) matches either (1) or (2),does not remember the match
  4. ([\-\.\s]) matches a dash, period or space character and remembers it
  5. \d{3} matches the next 3 digits
  6. \1 matches the dash, period or space matched previously in (4)
  7. d{4} matches the last 4 digits

Look at the Javascript in the regular expression practice program.