Home > Software engineering >  Replace string between two time values
Replace string between two time values

Time:11-23

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!

CodePudding user response:

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);

CodePudding user response:

Try this: ^(\d [\d:.] \d )([-a-z\s] )(\d [\d:.] \d )$

demo here: https://regex101.com/r/2nLCut/1

Groups 1 and 3 are the times, and 2 is the captured variable (this means you can replace the entire match with group 1 and group 3 to eliminate the string in group 2)

  • Related