split()

You already know about the method split(), which creates an array from an input string and a delimiter string. Let's take a string of comma-separated values and split it:

var csv = 'one, two,three ,four';

csv.split(',');
["one", " two", "three ", "four"]

Because the input string has some inconsistent spaces before and after the commas, the array result has spaces too. With a regular expression, we can fix this, using \s*, which means "zero or more spaces":

csv.split(/\s*,\s*/)

["one", "two", "three", "four"]
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents