admin管理员组

文章数量:1410682

I am trying to break up this string by first splitting it into sections divided by ';'. Then I want to split those sections divided by ','. It is not working though and I am about to break my puter. Could someone please help me figure this out.

You can play around with my jsfiddle if you want... /

var myString = "Call 1-877-968-7762 to initiate your leave.,-30,0,through;You are eligible to receive 50% pay.,0,365,through;Your leave will be unpaid.,365,0,After;";

var mySplitResult = myString.split(";");

for(i = 0; i < mySplitResult.length -1; i++){
    var mySplitResult2 = i.split(",");
    for(z = 0; z < mySplitResult2.length -1; i++) {
    //document.write("<br /> Element " + i + " = " + mySplitResult[i]);
        document.write("<br/>Element" + z + " = " + mySplitResult[z]);
    }
}

I am trying to break up this string by first splitting it into sections divided by ';'. Then I want to split those sections divided by ','. It is not working though and I am about to break my puter. Could someone please help me figure this out.

You can play around with my jsfiddle if you want... http://jsfiddle/ChaZz/

var myString = "Call 1-877-968-7762 to initiate your leave.,-30,0,through;You are eligible to receive 50% pay.,0,365,through;Your leave will be unpaid.,365,0,After;";

var mySplitResult = myString.split(";");

for(i = 0; i < mySplitResult.length -1; i++){
    var mySplitResult2 = i.split(",");
    for(z = 0; z < mySplitResult2.length -1; i++) {
    //document.write("<br /> Element " + i + " = " + mySplitResult[i]);
        document.write("<br/>Element" + z + " = " + mySplitResult[z]);
    }
}
Share Improve this question asked Oct 26, 2012 at 18:29 John DoeJohn Doe 2,0709 gold badges33 silver badges54 bronze badges 1
  • for(z = 0; z < mySplitResult2.length -1; i++) { you probably meant z++ there. – jbabey Commented Oct 26, 2012 at 18:40
Add a ment  | 

2 Answers 2

Reset to default 5

i is a number, as that's how you defined it.

To split the string, you need to access the i member of the Array.

var mySplitResult2 = mySplitResult[i].split(",");

If I may, if you have to split with character a then character b, the simplest would be : string.split('a').join('b').split('b')

本文标签: htmlSplitting string 2 times with javascriptStack Overflow