Prozrachniy Prozrachniy - 6 days ago 6
Javascript Question

How to split a string log file which starts by date in square brackets

I want to split a log string into separate logs into an array, the string looks like this:

'[2016-12-01 11:24:28.622] [INFO] Scraper - Processing year 2016 [2016-12-01 11:24:28.622] [INFO] Scraper - Successfully proccessed ....'

I tried using
.split()
function, but managed only to split by
[INFO]
.
Is there a regexp to use to split by date in square brackets ?
I'm new to regexp, would much appreciate any suggestions, thanks.
The desired outcome looks like this

['[2016-12-01 11:24:28.622] [INFO] Scraper - Processing year 2016', '[2016-12-01 11:24:28.622] [INFO] Scraper - Successfully proccessed ....']

Answer

Try

yourString.split(/(?=\[\d)/)

This splits your string at every [<number> (\[\d) without consuming those themselves - look-ahead ((?=...))