我正在开发一个系统,将评论记录在一个大评论文本框中。我需要将这些部分分开,以使它们在前端看起来更漂亮......因为现在它太可怕了。
但是,我似乎无法抓取数据并使用javascript / jquery将其拆分。
数据就像这样出来,每次都像这样格式化。
"Fri Jan 16 12:36:47 EST 2015
Entered by username - Persons name
This is the test comment
Sat Jan 17 2:16:00 EST 2015
Entered by username - Persons name
And this us another comment that could be very long and very redundant because these comments can be like that."
所以我需要把你分成三个不同的部分。日期,由评论输入,然后是评论。
我尝试过进行字符串拆分,但即使我尝试\n
或\s
任何帮助都会很可爱。 https://jsfiddle.net/wz5z2dzo/1/
答案 0 :(得分:1)
引用字符串(单个或双重)不支持文字新行。尝试使用模板文字。我不确定为什么按特定令牌拆分对你不起作用......
以下是一个解决方案。我逐行拆分,删除空行,然后迭代3秒。
const nar = `Fri Jan 16 12:36:47 EST 2015
Entered by username - Persons name
This is the test comment
Sat Jan 17 2:16:00 EST 2015
Entered by username - Persons name
And this us another comment that could be very long and very redundant because these comments can be like that.`;
const lines = nar.split( "\n" ).filter( line => line );
const comments = [];
for (let i = 0; i < lines.length; i += 3)
comments.push( {
date: lines[ i ],
name: lines[ i + 1 ].split( " - " )[ 1 ],
comment: lines[ i + 2 ]
} );
console.log( comments );
&#13;
这假设评论只有一行。如果他们这样做会有点复杂。