title: Seperating text into separate elements using a pattern - javascript
body: <p>Apologies in advance for my atrocious code / attempts at explaining what I'm trying to achieve...</p>
  <p>I want to take various transcripts, with time stamps, and convert it to a consistent format for creating subtitles. The transcripts are not from the same sources, and the structure of the documents and the timestamps vary, sometimes even within the same document.</p>
  <p>The format of the timestamp is [HH:MM:SS.FF] (the variations I can deal with) and it is contained within the text. And the timestamps <em>sometimes</em> indicate and end point (usually they are just a start point).</p>
  <p>So the format is</p>
  <pre><code>[Timestamp1]Some text with various line breaks and weird characters.
  [Timestamp2]More text where this transcript continues but ends with some silence after this
  [Timestamp3]
  [Timestamp4]The next sentence begins and ends at the last
  [Timestamp5]
  </code></pre>
  <p>What is the best coding approach to this in JavaScript? I've gone around the houses with string.split and re.matchAll but none of the regex patterns I come up with can deal with the 2 timestamps in a row.</p>
  <p>I think ideally I would have the regex pattern that gets the timestamp and then store an array of objects that have a Start and End timestamp (end is next start if end doesn't exist) and associated text.</p>
  <p>So for the above example I'd have</p>
  <pre><code>Start: Timestamp1 End: Timestamp2 Text: &quot;Some text...&quot;
  
  Start: Timestamp2 End: Timestamp3 Text: &quot;More text...&quot;
  
  Start: Timestamp4 End: Timestamp5 Text: &quot;The next...&quot;
  </code></pre>
  <p>This is one of my latest attempts...</p>
  <pre><code>function test(){
          str = 
          `[09:35:10.00]
          1. Lorem ipsum...
          [09:35:13.11]
          [09:35:15.14]
          2. sed do eiusmod...
          [09:35:39.20]
          3. anim id est laborum...
          [09:35:43.17]`
  
          var re = /(?&lt;tc1&gt;\[?(?:[0-1][0-9]|2[0-3]|[0-9]):(?:[0-5][0-9]):(?:[0-5][0-9])(?:\.(?:[0-9]{2,3})?\]?))\s*(.*)\s*(?&lt;tc2&gt;\[?(?:[0-1][0-9]|2[0-3]|[0-9]):(?:[0-5][0-9]):(?:[0-5][0-9])(?:\.(?:[0-9]{2,3})?\]?))?.*/gm;
  
          const matches = str.matchAll(re);
          for (const match of matches) {
                  console.log(`Start TC:\n${match[1]}\nText:\n${match[2]}\nTC2:\n${match[3]}`);
          }
  }
  </code></pre>
  <p>Which doesn't cater for the variations, unfortunately.</p>
  <p>Thanks for any pointers in the right direction.</p>