javascript由whitespaces 3元素拆分

时间:2014-10-09 14:13:38

标签: javascript

我有一些线...... 例如:

var line;
line = "INFO  2014-10-08 15:01:17,233 [localhost-startStop-1] Main directory: C:\WORKSPACE";
line = "ERROR 2014-10-08 15:01:16,646 [localhost-startStop-1] Cannot alter it is not a table.";
var headerArray = line.split("\\s", 3);
//headerArray returns array of 1 element "INFO  2014-10-08 15:01:17,233 [localhost-startStop-1] Main directory: C:\WORKSPACE"; don't know why..

但我只想要{" INFO"," 2014-10-08"," 15:01:17,233"}然后{&#34 ;错误"," 2014-10-08"," 15:01:16,646"}

1 个答案:

答案 0 :(得分:2)

删除其余部分后,您可以拆分。

var line = "ERROR 2014-10-08 15:01:16,646 [localhost-startStop-1] Cannot alter it is not a table.";
var arr = line.replace(/\s+/g," ").replace(/\s\[.+$/,"").split(" ");
console.log(arr);