0

我正在寻找解析 SRT 文件并使用 .split() 找到一些函数但我得到一个空数组...

我认为问题来自我的 SRT 语法,但我不明白 /(?:^|\n\n)\d+\n|\n+$/g在 .split() 函数内部的含义。

这些是我发现的功能:

function srtTimeToSeconds(time) {
  var match = time.match(/(\d\d):(\d\d):(\d\d),(\d\d\d)/);
  var hours        = +match[1],
      minutes      = +match[2],
      seconds      = +match[3],
      milliseconds = +match[4];

  return (hours * 60 * 60) + (minutes * 60) + (seconds) + (milliseconds / 1000);
}

function parseSrtLine(line) {
  var match = line.match(/(\d\d:\d\d:\d\d,\d\d\d) --> (\d\d:\d\d:\d\d,\d\d\d)\n(.*)/m);

  return {
    start: srtTimeToSeconds(match[1]),
    end:   srtTimeToSeconds(match[2]),
    text:  match[3].trim()
  };
}

function parseSrt(srt) {
  var lines = srt.split(/(?:^|\n\n)\d+\n|\n+$/g).slice(1, -2);

  return $.map(lines, parseSrtLine);
}

这是我的 SRT 字符串:

const mySRT =
"0\
00:00:01,414 --> 00:00:03,613\
All the single ladies\
1\
00:00:03,805 --> 00:00:05,904\
All the single ladies\
2\
00:00:06,118 --> 00:00:08,717\
All the single ladies\"

这是我的 Javascript 代码:

const subtitles = parseSrt(mySRT);
console.log(subtitles);

任何人都可以帮助我吗?

4

0 回答 0