1

I have a long string from a .txt-file, containing several sentences and dates that I want to split up.

The syntax is like this

01-01-15: Here is some text
02-01-15: Here is some other text
05-06-17: Here is some new text
06-06-17: Here is some text
taking up 
several lines
07-06-17: And so on

Since some of the sentences take up several lines I cannot use text.split("\n") - but what can I do instead then? All sentences start with xx-xx-xx where x is a number.

5
  • You can have a special symbol after at the end of each sentence and split by them Commented Aug 19, 2017 at 11:04
  • 2
    The Javascript 'split' function as you are using above can take a regular expression to split on (See developer.mozilla.org/en/docs/Web/JavaScript/Reference/…) . A quick google should help you if your unfamiliar with the syntax. If you wish to test there are many regex tester sites (such as regextester.com) Commented Aug 19, 2017 at 11:09
  • Possible duplicate of stackoverflow.com/questions/28633948/… Commented Aug 19, 2017 at 11:14
  • (How) can you be sure, that the sentences don't contain dates after new lines by chance? Commented Aug 19, 2017 at 11:25
  • Possible duplicate of Split using RegEx in JavaScript Commented Aug 19, 2017 at 12:06

1 Answer 1

5

You could split the lines by searching for the starting date of each logical unit with a lookahead pattern.

var data = '01-01-15: Here is some text\
02-01-15: Here is some other text\
05-06-17: Here is some new text\
06-06-17: Here is some text\
taking up \
several lines\
07-06-17: And so on';


console.log(data.split(/(?=\d\d-\d\d-\d\d:)/));

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

1 Comment

Exactly what I came up with, except I used \d{2}. \d\d is probably clearer.

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.