3

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.

11
  • Something like let [_, day, month, year] = f.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/);? Commented Aug 28, 2018 at 22:38
  • I don't think so becuase i would like to split it into a single list where each element is a message which starts with the dd/mm/yyyy Commented Aug 28, 2018 at 22:41
  • let res = f.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/).slice(1); ? Note .split(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/).filter(Boolean) works, too. Commented Aug 28, 2018 at 22:43
  • I get an error with the first one and the second one still isnt working ¬let res = f.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/).slice(1); ^ TypeError: Cannot read property 'slice' of null at Object.<anonymous>.... Commented Aug 28, 2018 at 22:48
  • Can't you just split per line? I don't see it as a significant timeloss. Otherwise wouldn't /g be missing? Commented Aug 28, 2018 at 22:48

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.