15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

478<br />

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

stringObject.search()<br />

Listing 34-4 (continued)<br />

function doSearch(form) {<br />

var replaceStr = form.replaceEntry.value<br />

var delim = (form.caseSens.checked) ? “/g” : “/gi”<br />

var regexp = eval(“/” + form.regexp.value + delim)<br />

form.result.value = mainString.search(regexp)<br />

}<br />

<br />

<br />

<br />

String Replace and Search with Regular Expressions<br />

<br />

Text used for string.replace() and string.search() methods:<br />

To be, or not to be: that is the question:<br />

Whether ‘tis nobler in the mind to suffer<br />

The slings and arrows of outrageous fortune,<br />

Or to take arms against a sea of troubles,<br />

And by opposing end them.<br />

<br />

Enter a regular expression to match:<br />

Case-sensitive<br />

Enter a string to replace the matching strings:<br />

<br />

<br />

<br />

Result:<br />

<br />

<br />

<br />

<br />

string.search(regExpression)<br />

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

Compatibility ✓ ✓ ✓ ✓ ✓<br />

Example<br />

Listing 34-4, for the string.replace() method, also provides a laboratory to<br />

experiment with the string.search() method.

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

Saved successfully!

Ooh no, something went wrong!