You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用JS移除WebVTT文件中第一个时间戳前的所有内容?

移除WebVTT文件中第一个时间戳前内容的正则解决方案

你需要移除WebVTT文件内第一个时间戳之前的所有内容,但之前用的正则表达式.replace(/(.*)\s+1/gmi, '')会误删首行的“1”,目标是保留“1”作为文件首行,仅删除其之前的内容。

原WebVTT文件内容

WEBVTT

1
00:00:00.080 --> 00:00:05.930
Lorem Ipsum is simply dummy text of the printing and typesetting industry. 

2
00:00:05.930 --> 00:00:07.430
Lorem Ipsum is simply dummy text of the printing and typesetting industry. 

3
00:00:07.430 --> 00:00:13.130
Lorem Ipsum is simply dummy text of the printing and typesetting industry. 

期望处理后内容

1
00:00:00.080 --> 00:00:05.930
Lorem Ipsum is simply dummy text of the printing and typesetting industry. 

2
00:00:05.930 --> 00:00:07.430
Lorem Ipsum is simply dummy text of the printing and typesetting industry. 

3
00:00:07.430 --> 00:00:13.130
Lorem Ipsum is simply dummy text of the printing and typesetting industry. 

正确正则表达式及用法

使用以下正则表达式进行替换,可精准匹配并删除“1”之前的所有内容,同时保留“1”行:

const cleanedContent = webvttContent.replace(/^[\s\S]*?(?=^1$)/m, '');

正则说明

  • ^:开启多行模式后,匹配字符串开头或行开头
  • [\s\S]*?:非贪婪匹配任意字符(含换行),避免过度匹配后续内容
  • (?=^1$):正向预查,确保匹配截止到单独一行的“1”之前,不会包含“1”本身

内容的提问来源于stack exchange,提问作者maTe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 22:20:11