如何将一串短信解析为一个对象
我想将模板字符串中包含的文本消息解析为以下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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样?
like this?
我会将整个字符串分成小块,例如:让字符串=日期+作者+内容。这样就简单多了。或者使用诸如 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()
这样的东西?
Something like this?