23.04.2013 Views

javascript

javascript

javascript

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Figure 2–30. Passing a RegExp for a literal string to match() and search()<br />

CHAPTER 2 ■ TYPE CONVERSION<br />

Note, however, that, when JavaScript converts a string to a RegExp object, the g, i, and m flags, which<br />

we’ll explore in Chapter 5, are not set. There’s no way for JavaScript to save the day if we intended to<br />

pass /jack/ig but instead passed "jack" to match(), as Figure 2–31 displays:<br />

var incredibles = "Mr. Incredible, Elastigirl, Violet, Dash, Jack-Jack";<br />

incredibles.match(/jack/ig);<br />

// ["Jack", "Jack"]<br />

incredibles.match("jack");<br />

// null<br />

Note that match() conveys failure, which is to say no array of matching strings, by returning null.<br />

Remember from Chapter 1 that null conveys no value on the heap, in other words, no object, array, or<br />

function. That is why match() returned null instead of undefined.<br />

55

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

Saved successfully!

Ooh no, something went wrong!