I want to split a comma separated string with JavaScript. How?
4 Answers
var partsOfStr = str.split(',');
5 Comments
alex
@Andrew That's an entirely different question. Please read this one again.
Jonathan
remove any garbage (") server-side before its presented to JS
Jack
& you can use
.join() to put them back together againTenzolinho
@alex what if I have in str
{a: 'a'} how can I stop the split function to split this object, and only work on a list of elements?Marc
fails miserably when a field value contains a comma. obviously.
var result;
result = "1,2,3".split(",");
console.log(result);
More info on W3Schools describing the String Split function.
','is similar enough to splitting on'~'that it seems like this really is a duplicate.