Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replace string between two time values

I am trying to clean up broken VTT files, where the lines show:
00:00.000 -- constituent 00:06.880 but instead should show
00:00.000 --> 00:06.880

VTT is written so that it’s MM:SS:MSMSMS, and minutes can be any value, so I tried to match that via a regexp using ^\d+\:\d+\.\d+$, which apparently should work and on some regexp testing places it matches at first, but then when I add additional content to the string the match fails.

How can I get the string between these two matches so that I can replace it with --> ? The word here (constituent) is variable and so I need a general regexp rather than just a match and replace for the string. Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could use this regex and replace code:

const input = `
1
00:00.000 -- constituent 00:06.880

2
00:30.022 test-test 00:37.750`;

const result = input.replace(/^(\d[\d:.]+\d)\b.*?\b(\d[\d:.]+\d)$/gm, "$1 --> $2");

console.log(result);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading