首页 > 解决方案 > 拆分前两个空格的字符串

问题描述

我有这个字符串

var str = "394987011016097814 1d the quick brown fox jumped over the lazy dog";

..我正试图让它成为这个数组

[
    "394987011016097814",
    "1d",
    "the quick brown fox jumped over the lazy fox",
]

我已经在第一个空格出现时看到了这个答案拆分字符串,但这仅适用于第一个空格。

标签: javascriptstringsplit

解决方案


使用拆分和连接进行解构

var str = "394987011016097814 1d the quick brown fox jumped over the lazy fox";
var [str1, str2, ...str3] = str.split(' ');
str3 = str3.join(' ');
console.log([str1, str2, str3])


推荐阅读