Skip to content

Instantly share code, notes, and snippets.

@brospars
Last active December 12, 2022 12:27
Show Gist options
  • Save brospars/0bd13de8a22530c87d0945cf8e611225 to your computer and use it in GitHub Desktop.
Save brospars/0bd13de8a22530c87d0945cf8e611225 to your computer and use it in GitHub Desktop.
Regular expression to match vtt subtitles

Regex : /^(\d{2}:\d{2}:\d{2}[.,]\d{3})\s-->\s(\d{2}:\d{2}:\d{2}[.,]\d{3})\n(.*(?:\r?\n(?!\r?\n).*)*)/gm
Demo : https://regex101.com/r/TH1JhU/1
Code example :

const regex = /^(\d{2}:\d{2}:\d{2}[.,]\d{3})\s-->\s(\d{2}:\d{2}:\d{2}[.,]\d{3})\n(.*(?:\r?\n(?!\r?\n).*)*)/gm;
const str = `WEBVTT

00:00:00.000 --> 00:00:04.469
Blah Bla bla 1
Multine
- example

00:00:04.469 --> 00:00:46.485
Blah Bla bla 2

00:00:46.485 --> 00:01:48.465
Blah Bla bla 3

00:01:48.465 --> 00:02:48.360
Blah Bla bla 4

`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    console.table(m)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment