我有string
看起来像这样(字幕文件):
"1\n00:00:27,560 --> 00:00:29,990\nHandelingen 19:5\n\"En toen zij dit hoorden",
我希望它成为这样的array
(长度为3):
var array = [
"1",
"00:00:27,560 --> 00:00:29,990",
"Handelingen 19:5 \"En toen zij dit hoorden"
]
这是我尝试过的,但我没有比这更进一步。
// I putted \n in to act as the linebreaks.
var string = "1\n00:00:27,560 --> 00:00:29,990\nHandelingen 19:5\n\"En toen zij dit hoorden,";
// I did not get any further than this :/
var chunks = string.split('\n');
console.log(chunks);

如何拆分前两行并让前两行之后的行加入。什么是最快/最有效的方法呢?段落数量可以增加到2500个。
答案 0 :(得分:1)
var string = "1\n00:00:27,560 --> 00:00:29,990\nHandelingen 19:5\n\"En toenzij dit hoorden,";
var chunks = string.split('\n', 2);
chunks[2] = string.substr(chunks[0].length+chunks[1].length+2,string.length);
//.replace(/\n/, ""); optional
console.log(chunks[0]);
console.log(chunks[1]);
console.log(chunks[2]);

答案 1 :(得分:1)
我刚刚写了一个SRT子文件解析器。运行代码段以查看结果,您感兴趣的功能是parseSub
和parseSubs
function parseSub(sub) {
sub = sub.split(/\r*\n/);
var line1 = sub[0],
line2 = sub[1].split(/\s*-->\s*/),
start = line2[0],
end = line2[1],
text = sub.slice(2).join('');
return {
index: parseInt(line1),
from : start,
to : end,
text : text
};
}
function parseSubs(fileText) {
return fileText.trim().split(/\r*\n\s+/).map(function(subtext) {
return parseSub(subtext);
});
}
var subsText = document.getElementById('subs')
subsText.textContent = JSON.stringify(parseSubs(subsText.textContent), null, 2);

<pre id="subs">1
00:00:00,800 --> 00:00:04,620
Mr. De Wever, je vous rends la parole dans un instant. J'écoute d'abord Mr. Smet.
2
00:00:04,620 --> 00:00:09,220
Vous l'avez entendu: la médiocrité, un amalgame 'd'unité',
3
00:00:09,220 --> 00:00:14,340
tout doit être chouette. Je peux quelque part comprendre la préoccupation de la N-VA.
4
00:00:14,340 --> 00:00:16,000
Oh mais je ne comprends pas seulement l'inquiétude de la N-VA,
</pre>
&#13;
答案 2 :(得分:0)
像字符串那样的标准字符串是否会始终具有相同类型的数据?如果是,为什么不将它拆分,然后将数组的最后两个元素组合在一起并存储在变量中,然后删除索引2,3处的最后2个元素,然后将变量添加到数组中。
// I putted \n in to act as the linebreaks.
var string = "1\n00:00:27,560 --> 00:00:29,990\nHandelingen 19:5\n\"En toen zij dit hoorden,";
// I did not get any further than this :/
const [id, timestamp, whatever, whatever2] = string.split("\n");
var array = [
id,
timestamp,
whatever+whatever2
]
console.log(array);
&#13;
答案 3 :(得分:0)
在@Tyblitz和@JaredT的帮助下,我设法解决了这个问题。使用.slice()
和.join()
// I putted \n in to act as the linebreaks.
var string = "1\n00:00:27,560 --> 00:00:29,990\nHandelingen 19:5\n\"En toen zij dit hoorden,";
// I did not get any further than this :/
var chunks = string.split('\n');
var array = [];
array.push(
chunks.slice(0, 1).join(),
chunks.slice(1, 2).join(),
chunks.slice(2, chunks.length).join()
);
console.log(array);
&#13;