Search code examples
javascriptregexstringsplitsrt

Regex Or split based on number


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!


Solution

  • 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.