正在分析字符串以获取与单词相关联的时间戳数组

Parsing String to get an array of time stamps associated to words

本文关键字:关联 单词相 时间戳 数组 获取 字符串      更新时间:2023-09-26

我有一个字符串,下面有字幕文本。假设我有一个单词数组[first,line,two],我想获得一个与这些单词第一次出现时相关的时间戳数组,如下所示[00:01:00,00:00:15.000]

我想知道如何做到这一点,我想我可以做的是,当这个词出现时,试着找到"-->"的最新实例,然后只得到最后12个字符,这将是时间戳。我只是不能100%确定如何通过JS实现这一点。我想它是用索引来表示位置的吧?

WEBVTT
1
00:00:01.000 --> 00:00:10.000
This is the first line of text, displaying from 1-10 seconds
2
00:00:15.000 --> 00:00:20.000
And the second line of text
separated over two lines

可以从拆分函数开始:

var str = "00:00:01.000 --> 00:00:10.000";
var res = str.split(" --> ");
document.getElementById("demo").innerHTML = res + "<br />";
document.getElementById("demo").innerHTML += res[0] + "<br />";
document.getElementById("demo").innerHTML += res[1];
// result:
// 00:00:01.000,00:00:10.000
// 00:00:01.000
// 00:00:10.000