Javascript split text from file to array with Regex based on DD/MM/YYYY

Viewed 424

I am trying to split an imported text file into an array based on the fact each line starts with a date in the format DD/MM/YYYY. I have tried using regex to achieve this:

flist = f.split(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/)

with f being the string to split. However the code runs and produces an array saved to flist and when console.log(flist) is run it only has one element and has not been split up.

edit:

Full code:

const fs = require("fs")

f = fs.readFileSync("file.txt", "utf8")
let flist = f.split(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/g)
console.log(flist)

example file.txt:

18/07/2018, 18:04 - Person2: message
18/07/2018, 18:04 - Person1: Yes
18/07/2018, 18:04 - Person2: That's good then
18/07/2018, 18:05 - Person1: message line 1
 message line 2
18/07/2018, 18:05 - Person2: text
18/07/2018, 18:05 - Person2: But nvm
18/07/2018, 18:06 - Person1: text

So the issue with splitting with new line is that a new line doesn't mean a new message however i want my array to be each new message so therefore need each new element to start with DD/MM/YYYY an am searching to split with that with regex however it is not splitting/finding a match.

1 Answers

You may split with

var flist = f.split(/(?=^\d{1,2}\/\d{1,2}\/\d{4})/m).filter(Boolean)

See the regex demo

The (?=^\d{1,2}\/\d{1,2}\/\d{4}) pattern matches a location that is right at the start of a line (m modifier makes ^ match start of a line) that is followed with 1 or 2 digits, /, 1 or 2 digits, / and 4 digits. The .filter(Boolean) part will remove empty items.

JS demo:

var f = "18/07/2018, 18:04 - Person2: message\n18/07/2018, 18:04 - Person1: Yes\n18/07/2018, 18:04 - Person2: That's good then\n18/07/2018, 18:05 - Person1: message \nine 1\n message line 2\n18/07/2018, 18:05 - Person2: text\n18/07/2018, 18:05 - Person2: \nut nvm\n18/07/2018, 18:06 - Person1: text";
var flist = f.split(/(?=^\d{1,2}\/\d{1,2}\/\d{4})/m).filter(Boolean);
console.log(flist);

Related