spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column57


The Doc Dialer

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

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;
  return(parseFloat(navigator.appVersion.substr(msieIndex,3)));
}

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.

http://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint


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

URL: http://www.webreference.com/js/column57/9.html