The Doc Dialer: IE 5.5's Replace Function | WebReference

The Doc Dialer: IE 5.5's Replace Function

The Doc Dialer

IE 5.5's Replace Function

In this page we explain the enhanced replace() function, supported by Internet Explorer 5.5 and above. On the previous page we showed you what does extractFileName() do:

function extractFileName(empName) {
  if (nameCode == 1 && versionCode >= 5.5) {
    var regExp = /(\w+)\s*(\w+)/g;
    return empName.replace(regExp, matchingFunction);
  else {
    blankPos = empName.indexOf(" ");
    firstName = empName.substr(0, blankPos);
    lastName = empName.substr(blankPos + 1, empName.length - blankPos);
    return firstName + lastName;

The objective of this function is to generate a file name out of a president's first and last names. We first define a regular expression:

var regExp = /(\w+)\s*(\w+)/g;

The regular expression will match a string that includes a word, followed by a zero number or more of blanks, and ends with a word. The flag /g allows for more than one matches of the above sequence. The replace() function takes two parameters: the regular expression and a handling function (called here matchingFunction()). Our matchingFunction() is defined as:

function matchingFunction(matchedString, subMatch1, subMatch2, matchPos, source)
 {return (subMatch1 + subMatch2)

A submatch is found according to the parenthesis in the regular expression. The first pair defines subMatch1, the second pair defines subMatch2, and so on. According to the regular expression above, the first pair encloses the first word and the second pair of parenthesis encloses the second word. For example, subMatch1 for Bill Clinton is Bill, while subMatch2 is Clinton. The parameter matchPos is the position within the source string that the match was found. The parameter source is the source string. The number of pairs of parenthesis in the regular expression must match the number of submatches parameters passed to the matchingFunction() above.

You can do anything inside the matching function. At the end you have to return a string. In our case above, we return a concatenation of the submatches, yielding the president's first name concatenated with his last name. The replace() function takes the string returned by the matching function and substitutes it with the substring matched by the regular expression. In our case, the regular expression matches the whole string of the president name, and the replace() function replaces it with a concatenation of the first and last names.

Just a reminder how to find the Internet Explorer version number:

function bVer() {
  // return version number (e.g., 4.03)
  msieIndex = navigator.appVersion.indexOf("MSIE") + 5;

Extracting the version number is not that trivial. The number at the head of the navigator.appVersion string is still 5.0. We need to get deeper into the string and find the substring "MSIE". The version number appears just after it.

Produced by Yehuda Shiran and Tomer Shiran

Created: February 14, 2000
Revised: February 14, 2000