javascriptregexwebvttvtt

Remove everything before first timestamp in webvtt file - JS


I try to remove everything before first timestamp in .vtt file.

I made something .replace(/(.*)\s+1/gmi, '') but it select "1" as well. My expecting is to select everything except "1" which should be a first line in file.

WEBVTT

1
00:00:00.080 --> 00:00:05.930
Lorem Ipsum is simply dummy text of the printing and typesetting industry. 

2
00:00:05.930 --> 00:00:07.430
Lorem Ipsum is simply dummy text of the printing and typesetting industry. 

3
00:00:07.430 --> 00:00:13.130
Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
1
00:00:00.080 --> 00:00:05.930
Lorem Ipsum is simply dummy text of the printing and typesetting industry. 

2
00:00:05.930 --> 00:00:07.430
Lorem Ipsum is simply dummy text of the printing and typesetting industry. 

3
00:00:07.430 --> 00:00:13.130
Lorem Ipsum is simply dummy text of the printing and typesetting industry. 

Solution

  • Use

    .replace(/^.*\s+(?=^1$)/m, '')
    

    I removed global flag because this expression must remove from start of string only. m is preserved, it makes both anchors be applied at start and end of a line. Capturing parentheses are not necessary.

    (?=^1$) ensures the match comes before a line with a single 1 on it.

    See regex proof.