Gwalk
Gwalk

Reputation: 209

javascript split by whitespaces 3 elements

I have some lines.. For example:

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..

But I want only {"INFO", "2014-10-08", "15:01:17,233"} and then {"ERROR", "2014-10-08", "15:01:16,646"}

Upvotes: 0

Views: 30

Answers (1)

Amit Joki
Amit Joki

Reputation: 59232

You can just split after removing the rest.

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

Upvotes: 2

Related Questions