如何将一串短信解析为一个对象

发布于 2025-01-18 18:22:29 字数 647 浏览 0 评论 0原文

我想将模板字符串中包含的文本消息解析为以下JS对象。每个短信都被一条新线路分开,在作者命名之后,有一个结肠。消息的内容还可以包括新线条,方括号和结肠。您首选解决这个问题是什么?

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() {
  // ?
}

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() {
  // ?
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

阳光①夏 2025-01-25 18:22:29

像这样?

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}

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}

淡淡離愁欲言轉身 2025-01-25 18:22:29

我会将整个字符串分成小块,例如:让字符串=日期+作者+内容。这样就简单多了。或者使用诸如 string.split() 之类的东西

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

踏雪无痕 2025-01-25 18:22:29

这样的东西?

function parseMessage(message) {
   var messageObj = {};
   var messageArray = message.split(" ");
   messageObj.command = messageArray[0];
   messageObj.args = messageArray.slice(1);
   return messageObj;
}

Something like this?

function parseMessage(message) {
   var messageObj = {};
   var messageArray = message.split(" ");
   messageObj.command = messageArray[0];
   messageObj.args = messageArray.slice(1);
   return messageObj;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文