Text File To Json, Last Few Lines Are Not Included In The Result
I'm reading a text file and converting it to JSON format using regex in my react project.It is working fine but not including last 20-30 lines of the text file. There is some probl
Solution 1:
Am not certain about the issue with the existing code at Question.
To get expected result described at Question utilizing an alternative approach you can use RegExp
/\s{2,}|\n+/g
to replace space characters greater than 2 and new line characters; /[\d-]+\s[\d:]+/g
to get dates; /.+(?=\s\w+\s$|\s\w+$)|\w+\s$|\w+$/g
to match text that is followed by space, word characters and space character or end of string and characters before space characters followed by word characters and space character or end of string; return an object with a property set for each element of the array from .map()
let allText = `2014-06-01 23:07:58 President Resigns in Georgia’s Breakaway Region of
Abkhazia t.co/DAploRvCvV nytimes
2014-06-01 23:48:06 The NYT FlipBoard guide to understanding climate
change and its consequences t.co/uPGTuYiSmQ nytimes
2014-06-01 23:59:06 For all the struggles that young college grads
face, a four-year degree has probably never been more valuable
t.co/Gjf6wrwMsS nytimes
2014-06-01 23:35:09 It's better to be a community-college graduate than
a college dropout t.co/k3CO7ClmIG nytimes
2014-06-01 22:47:04 Share your experience with Veterans Affairs health
care t.co/PrDhLC20Bt nytimes
2014-06-01 22:03:27 Abandon Hope, Almost All Ye Who Enter the N.B.A.
Playoffs t.co/IQAJ5XNddR nytimes`;
// replace more than one consecutive space character and new line characters
allText = allText.replace(/\s{2,}|\n+/g, " ");
// get dateslet dates = allText.match(/[\d-]+\s[\d:]+/g);
// get characters that are not dates// spread `dates` to resulting array// return objectlet res = allText
.split(/[\d-]+\s[\d:]+\s/)
.filter(Boolean)
.map((text, index) =>
[dates[index], ...text.match(/.+(?=\s\w+\s$|\s\w+$)|\w+\s$|\w+$/g)])
.map(([date, text, user_id]) => ({date, text, user_id}));
console.log(res);
Post a Comment for "Text File To Json, Last Few Lines Are Not Included In The Result"