JS 文本转数组

发布于 2024-10-09 18:54:33 字数 514 浏览 6 评论 0原文

我有这样的文字:

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

我需要它是这样的:

// 2/92/20
chars[0][0]=2;
chars[0][1]=92;
chars[0][2]=20;

我该怎么做? PS 分割必须位于:

$.ajax({   
    type:     "POST",
    url:      "char_info2.php",   
    dataType: "html",  
    success:  function(data) 
              {
                  //here
              }
});

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
              }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流星番茄 2024-10-16 18:54:33

如果您有一个带有换行符的字符串,则可以使用 split< /code>:

var chars = str.split("\n");
for(var i = chars.length; i--; ) {
    chars[i] = chars[i].split('/');
}

DEMO

如果你想拥有它们作为整数,您将不得不再次循环它们。

但我同意这些评论,将它们作为 JSON 发送更有意义!

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!

葬心 2024-10-16 18:54:33

您可以使用 split 或正则表达式:

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 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]);
    }
});
茶底世界 2024-10-16 18:54:33

您可以在换行符和斜杠上分割

http://www.w3schools.com/jsref/jsref_split.asp< /a>

You could split on newline and slash

http://www.w3schools.com/jsref/jsref_split.asp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文