Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
How can I parse a string of text messages into an object

I want to parse text messages contained in a template string to the following JS object. Every text message is seperated by a new line and after the authors name there is a colon. The content of the message can also include new lines, square brackets and colons. What is your preferred way of solving this?

let string = `[03.12.21, 16:12:52] John Doe: Two questions:
How are you? And is lunch at 7 fine?
[03.12.21, 16:14:30] Jane Doe: Im fine. 7 sounds good.`;

let data = {
  "03.12.21, 16:12:52": {
    "author": "John Doe",
    "content": "Two questions:\nHow are you? And is lunch at 7 fine?"
  },
  "03.12.21, 16:14:30": {
    "author": "John Doe",
    "content": "Im fine. 7 sounds good."
  },
}; // will be parseString()

function parseString() {
  // ?
}

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

like this?

let string = `[03.12.21, 16:12:52] John Doe: Two questions:
How are you? And is lunch at 7 fine?
[03.12.21, 16:14:30] Jane Doe: Im fine. 7 sounds good.`;

let chunks = string.split(/^\[(\d\d\.\d\d\.\d\d, \d\d\:\d\d\:\d\d)\](.*?):/m);

let data = {};
for (let i = 3; i < chunks.length; i += 3) {
  const date = chunks[i - 2];
  const author = chunks[i - 1].trim();
  const content = chunks[i].trim();
  data[date] = {
    author,
    content,
  };
}

console.log(data);
.as-console-wrapper{top:0;max-height:100%!important}

about 4 years ago · Juan Pablo Isaza Report

0

Something like this?

function parseMessage(message) {
   var messageObj = {};
   var messageArray = message.split(" ");
   messageObj.command = messageArray[0];
   messageObj.args = messageArray.slice(1);
   return messageObj;
}
about 4 years ago · Juan Pablo Isaza Report

0

I would split the to whole string into little pieces like: let string = date + author + content. With that its much simpler. Or use something like string.split()

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!