7

I can use JavaScript's split to put a comma-separated list of items in an array:

var mystring = "a,b,c,d,e";
var myarray = mystring.split(",");

What I have in mind is a little more complicated. I have this comma separated string:

"mystring_109_all,mystring_110_mine,mystring_125_all"

how do i split this string in to an array

3
  • 6
    Please clarify how the array should look like. Commented Aug 23, 2010 at 14:09
  • 1
    The same as you already wrote in your first example. What should the result be? Commented Aug 23, 2010 at 14:09
  • 1
    Unless I'm mistaken, there's really nothing more complicated. It's still a simple split on a comma. Commented Aug 23, 2010 at 14:09

7 Answers 7

11

You can provide a regular expression for split(), so to split on a comma or an underscore, use the following:

var mystring = "mystring_109_all,mystring_110_mine,mystring_125_all";
var myarray  = mystring.split(/[,_]/);

If you're after something more dynamic, you might want to try something like "Search and don't replace", a method of using the replace() function to parse a complex string. For example,

mystring.replace(/(?:^|,)([^_]+)_([^_]+)_([^_]+)(?:,|$)/g,
  function ($0, first, second, third) {
    // In this closure, `first` would be "mystring",
    // `second` would be the following number,
    // `third` would be "all" or "mine"
});
Sign up to request clarification or add additional context in comments.

5 Comments

shouldn't that be mystring.split(/,|_/);
Thanks for the link to the post - haven't seen that before. Good to know.
or even mystring.split(/[,_]/);
@Ken: yes, I meant to put [,_] - thanks for pointing that out, I'm a bit off my game today ;)
@Andy - gotta get your head back, dude
2

Same, but loop

var myCommaStrings = myString.split(','); 
var myUnderscoreStrings = []; 
for (var i=0;i<myCommaStrings.length;i++) 
  myUnderscoreStrings[myUnderscoreStrings.length] = myCommaStrings[i].split('_');

Comments

1

Throwing a wild guess, given your spec isn't complete:

var mystring = "mystring_109_all,mystring_110_mine,mystring_125_all";
var myarray = mystring.split(",");
for (var i = 0; i < myarray.length; i++) {
  myarray[i] = myarray[i].split("_");
}

Comments

1

If you want to split on commas and then on underscores, you'd have to iterate over the list:

var split1 = theString.split(',');
var split2 = [];
for (var i = 0; i < split1.length; ++i)
  split2.push(split1[i].split('_'));

If you want to split on commas or underscores, you can split with a regex, but that's sometimes buggy. Here's a page to read up on the issues: http://blog.stevenlevithan.com/archives/cross-browser-split

Comments

0

Umm, the same way you would your original example:

var mystring = "mystring_109_all,mystring_110_mine,mystring_125_all";
var myarray = mystring.split(",");

Comments

0

I've created a JavaScript functions-only library called FuncJS which has a function called split() which would (hopefully) get the task done.

Read the docs and do download FuncJS, it's a very lightweight file.

1 Comment

could you please give a code example on how exactly your library would simplify the task. Just saying it's somewhere in the docs is not that helpful. Especially if you consider that more people than just the author might see your answer. Than everybody has to take the additional steps to see why exactly your solution might be superior.
0
<!DOCTYPE html>
<html>
<body>

<p>Question is var str = "a,b,c,d,e,f,g"; </p>

<button onclick="myFunction()">Split Button</button>

<p id="demo"></p>
<p id="demo1"></p>

<script>
var text = "";
function myFunction() {
    var str = "a,b,c,d,e,f";
    var arr = str.split(",");

    for(var i = 0; i<arr.length; ++i){ 
        text += arr[i] +" ";
        document.getElementById("demo").innerHTML = text;
    }
    document.getElementById("demo1").innerHTML = arr[0];
}


</script>

</body>
</html>


**Answer**
a b c d e f g

Arr[0]=a

2 Comments

Answer a b c d e f g
Answer Arr[0]=a

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.