简体   繁体   English

如何将此字符串拆分为大块

[英]How to split this string into chunks

I have a string which looks like this (subtitle file): 我有一个看起来像这样的string (字幕文件):

"1\n00:00:27,560 --> 00:00:29,990\nHandelingen 19:5\n\"En toen zij dit hoorden",

I want it to become an array like this (with a length of 3): 我希望它成为这样的array (长度为3):

var array = [
    "1",
    "00:00:27,560 --> 00:00:29,990",
    "Handelingen 19:5 \"En toen zij dit hoorden"
]
  • The first array item is the paragraph number 第一个数组项是段落
  • The second array item is the timing of the subtitle 第二个数组项是字幕的时间安排
  • The thirth array item is the text content 第三个数组项是文本内容

This is what Ive tried, but I did not get any further than this. 这是我已经尝试过的方法,但是我对此没有更多的了解。

 // 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); 

How can I split the first two lines and let the lines after the first two join each other. 我怎么可以拆分前两行,并让前两后的线路连接对方。 And what is the fastest / most efficient way to do it? 最快/最有效的方法是什么? The amount of paragraphs can grow to 2500. 段落的数量可以增加到2500。

Is this what you mean? 你是这个意思吗? There is probably a better way of doing it, but this should work. 可能有更好的方法,但是应该可以。

  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]); 

I happened to have written an SRT sub file parser a while ago. 我刚巧写了一个SRT子文件解析器。 Run the code snippet to see the result, the function you are interested in are parseSub and parseSubs 运行代码片段以查看结果,您感兴趣的函数是parseSubparseSubs

 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> 

Is it standard string like the string is going to have same kind of data all the time? 是标准字符串,就像字符串将一直具有相同类型的数据一样吗? If yes why don't you split it and then combine the last two elements of array together and store in a variable and then remove the last 2 elements at index 2, 3 then add the variable to the array. 如果是,为什么不拆分它,然后将数组的最后两个元素组合在一起并存储在变量中,然后删除索引2、3的最后两个元素,然后将变量添加到数组中。

 // 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); 

With the help of the anwsers given by @Tyblitz and @JaredT I managed to resolve it. 在@Tyblitz和@JaredT给出的答案的帮助下,我设法解决了它。 Using .slice() and .join() 使用.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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM