I have this text:
2/92/20
3/32/32
4/62/6
5/22/28
6/60/61
7/33/32
8/34/31
9/31/19
10/19/19
11/34/39
12/32/32
14/19/25
15/45/37
16/32/32
17/84/36
18/72/33
And I need it to be like:
// 2/92/20
chars[0][0]=2;
chars[0][1]=92;
chars[0][2]=20;
How can I do that? P.S. The split must be in开发者_开发知识库:
$.ajax({
type: "POST",
url: "char_info2.php",
dataType: "html",
success: function(data)
{
//here
}
});
If you have one string with line breaks, you can use split
:
var chars = str.split("\n");
for(var i = chars.length; i--; ) {
chars[i] = chars[i].split('/');
}
DEMO
If you want to have them as integers you would have to loop over them again.
But I agree with the comments, sending them as JSON makes much more sense!
You can either use split or a regular expression:
var your_text = "2/92/20\n" +
"3/32/32\n" +
// ... etc ...
// Split Version
var line_by_line = your_text.split("\n");
var final_array = [];
for (var i=0; i < line_by_line.length; i++) {
final_array.push(line_by_line[i].split("/"));
}
// Regular Expression Version
var splitter = /^(\d+)\/(\d+)\/(\d+)$/gi;
var final_array = [];
your_text.replace(splitter, function(matched_string, match1, match2, match3) {
if (match1) { // Assumes 0 is not a valid 1st number
final_array.push([match1, match2, match3]);
}
});
You could split on newline and slash
http://www.w3schools.com/jsref/jsref_split.asp
精彩评论