Skip to content
Advertisement

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!

Advertisement

Answer

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);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement