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.
asked Aug 19, 2017 at 11:01
BeHaa
6091 gold badge8 silver badges12 bronze badges
-
You can have a special symbol after at the end of each sentence and split by themSuren Srapyan– Suren Srapyan2017年08月19日 11:04:11 +00:00Commented Aug 19, 2017 at 11:04
-
2The 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)Tim Johnson– Tim Johnson2017年08月19日 11:09:21 +00:00Commented Aug 19, 2017 at 11:09
-
Possible duplicate of stackoverflow.com/questions/28633948/…T.J. Crowder– T.J. Crowder2017年08月19日 11:14:08 +00:00Commented Aug 19, 2017 at 11:14
-
(How) can you be sure, that the sentences don't contain dates after new lines by chance?Stephan– Stephan2017年08月19日 11:25:17 +00:00Commented Aug 19, 2017 at 11:25
-
Possible duplicate of Split using RegEx in JavaScriptParveen Sachdeva– Parveen Sachdeva2017年08月19日 12:06:28 +00:00Commented Aug 19, 2017 at 12:06
1 Answer 1
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:)/));
answered Aug 19, 2017 at 11:10
Nina Scholz
388k26 gold badges367 silver badges417 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
T.J. Crowder
Exactly what I came up with, except I used
\d{2}. \d\d is probably clearer.lang-js