String.split()返回空数组

时间:2018-05-26 10:33:04

标签: javascript jquery parsing split srt

我正在寻找解析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);

有人可以帮我吗?

0 个答案:

没有答案