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.

CHAPTER 2 ■ TYPE CONVERSION<br />

42<br />

Figure 2–17. Where’d the array of substrings go?<br />

As noted earlier in the chapter, split(), like any other string method, does not modify the string<br />

value it’s called upon. Rather, split() returns a new value. We’d need to save the array to a new variable<br />

or overwrite villains. Let’s do the latter, verifying our work with Figure 2–18:<br />

var villains = "Green Goblin, Doctor Octopus, Venom, Hobgoblin, and Sandman";<br />

villains = villains.split(/, (?:and )?/g);<br />

villains[1];<br />

// "Doctor Octopus"<br />

Figure 2–18. Overwriting the string in villains with the array returned by split()

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

Saved successfully!

Ooh no, something went wrong!