15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

474<br />

<strong>JavaScript</strong> <strong>Examples</strong> <strong>Bible</strong>: The Essential Companion to <strong>JavaScript</strong> <strong>Bible</strong><br />

stringObject.match()<br />

string.lastIndexOf(searchString[,<br />

startIndex])<br />

NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5<br />

Compatibility ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓<br />

Example<br />

Enter each of the following statements (up to, but not including the “//” comment<br />

symbols) into the top text box of The Evaluator (you can simply replace the parameters<br />

of the lastIndexOf() method for each statement after the first one).<br />

Compare your results with the results shown below.<br />

a = “bananas”<br />

a.lastIndexOf(“b”) // result = 0 (index of 1st letter is zero)<br />

a.lastIndexOf(“a”) // result = 5<br />

a.lastIndexOf(“a”,1) // result = 1 (from 2nd letter toward the front)<br />

a.lastIndexOf(“a”,2) // result = 1 (start from 3rd letter working toward front)<br />

a.lastIndexOf(“a”,4) // result = 3 (start from 5th letter)<br />

a.lastIndexOf(“nan”) // result = 2 [except for -1 Nav 2.0 bug]<br />

a.lastIndexOf(“nas”) // result = 4<br />

a.lastIndexOf(“s”) // result = 6<br />

a.lastIndexOf(“z”) // result = -1 (no “z” in string)<br />

string.match(regExpression)<br />

NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5<br />

Compatibility ✓ ✓ ✓ ✓ ✓<br />

Example<br />

To help you understand the string.match() method, Listing 34-3 provides a<br />

workshop area for experimentation. Two fields occur for data entry: the first is for<br />

the long string to be examined by the method; the second is for a regular expression.<br />

Some default values are provided in case you’re not yet familiar with the syntax<br />

of regular expressions (see Chapter 38 of the <strong>JavaScript</strong> <strong>Bible</strong>). A check box lets<br />

you specify whether the search through the string for matches should be casesensitive.<br />

After you click the “Execute match( )” button, the script creates a regular<br />

expression object out of your input, performs the string.match() method on the<br />

big string, and reports two kinds of results to the page. The primary result is a<br />

string version of the array returned by the method; the other is a count of items<br />

returned.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!