-2

i have trouble splitting a vtt file which is chunked together as one string.

i have this string: "3 00:00:09.023 --> 00:00:11.953 Only by looking at her sitting with her legs spread widely, 4 00:00:11.953 --> 00:00:13.593 you can tell she's a troublemaker."

I want to make it like this

3

00:00:09.023 --> 00:00:11.953

Only by looking at her sitting with her legs spread widely,

4

00:00:11.953 --> 00:00:13.593

you can tell she's a troublemaker.

with every number(3,4...etc) as the new line. i several thousand numbers to split

appreciate if any 1 could help! thanks!

  • I'm not sure, but maybe: [`str = str.replace(/\d+ \d\d:\d\d:\d\d\.\d{3} --> \d\d:\d\d:\d\d\.\d{3}/g, '\n$&')`](https://regex101.com/r/NWXZT3/1) – Washington Guedes Feb 01 '17 at 15:00

2 Answers2

0

If your string structure is fixed, you may use this:

var yourString = "3 00:00:09.023 --> 00:00:11.953 Only by looking at her sitting with her legs spread widely, 4 00:00:11.953 --> 00:00:13.593 you can tell she's a troublemaker.";
var newString = yourString.replace(/\d+\s\d{2}/g,'\n$&');

if this code does not cover all the cases, you should give more details.

Mohamed Abbas
  • 2,228
  • 1
  • 11
  • 19
0

You can use a positive lookahead to match a number and space character that is followed by the first time (00:00:09.023) and replace that by a newline character (\n) and the matched digit.

var subtitleLines = str.replace(/(\d+\s(?=\d{2}:\d{2}:\d{2}))/)/g, "\n$1")

To be more secure, in case some of the strings happen to include the now matched substring, you can include more of the time portion, e.g.

/(\d+\s(?=\d{2}:\d{2}:\d{2}\.\d{3}\s-->))/
baao
  • 71,625
  • 17
  • 143
  • 203