亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

javascript中字幕的正則表達式中的變量行數

javascript中字幕的正則表達式中的變量行數

慕森卡 2023-10-14 19:00:53
我使用正則表達式隨時創建字幕數組,其中包括 [number, start, end, text]/(\d+)\n([\d:,]+)\s+-{2}\>\s+([\d:,]+)\n([\s\S]*?(?=\n{2}|$))/gm但問題是,在文本部分,如果行數為 2 或更多,則不會被讀取。在這里您可以看到相關圖片我不希望只有該部分中文本的第一行被視為文本,如果還有其他行,也應該被考慮。你幫助我真是幫了我一個大忙。感恩let subtitle = document.getElementById('subtitle').value;console.log(_subtitle(subtitle));function _subtitle(text) {        let Subtitle = text;        let Pattern = /(\d+)\n([\d:,]+)\s+-{2}\>\s+([\d:,]+)\n([\s\S]*?(?=\n{2}|$))/gm;        let _regExp = new RegExp(Pattern);        let result = [];        if (typeof (text) != "string") throw "Sorry, Parser accept string only.";        if (Subtitle === null) return Subtitle;        let Parse = Subtitle.replace(/\r\n|\r|\n/g, '\n');        let Matches;        while ((Matches = Pattern.exec(Parse)) != null) {result.push({                Line: Matches[1],                Start: Matches[2],                End: Matches[3],                Text: Matches[4],            })        }                return result;    }#warning{background-color:#e74e4e;color:#fff;font-family:Roboto;padding:14px;border-radius:4px;margin-bottom:14px}textarea{width:100%;min-height:100px;}<div id="warning">The output is on the console</div><textarea id="subtitle">100:00:00,000 --> 00:00:00,600Hi my friends200:00:00,610 --> 00:00:01,050In the first line, everything works properlyBut there is a problem in the second line that I could not solve :(300:00:01,080 --> 00:00:03,080But then everything is in order and good400:00:03,280 --> 00:00:05,280You do me a great favor by helping me. Thankful</textarea>
查看完整描述

1 回答

?
交互式愛情

TA貢獻1712條經驗 獲得超3個贊

替換/gm/g,否則, the$指的是第一個“Text”行的末尾,并且正則表達式不會嘗試匹配其后的任何內容:

let subtitle = document.getElementById('subtitle').value;

console.log(_subtitle(subtitle));


function _subtitle(text) {

  let Subtitle = text;

  let Pattern = /(\d+)\n([\d:,]+)\s+-{2}\>\s+([\d:,]+)\n([\s\S]*?(?=\n{2}|$))/g;

  let _regExp = new RegExp(Pattern);

  let result = [];


  if (typeof(text) != "string") throw "Sorry, Parser accept string only.";

  if (Subtitle === null) return Subtitle;


  let Parse = Subtitle.replace(/\r\n|\r|\n/g, '\n');

  let Matches;


  while ((Matches = Pattern.exec(Parse)) != null) {

    result.push({

      Line: Matches[1],

      Start: Matches[2],

      End: Matches[3],

      Text: Matches[4],

    })


  }


  return result;

}

#warning {

  background-color: #e74e4e;

  color: #fff;

  font-family: Roboto;

  padding: 14px;

  border-radius: 4px;

  margin-bottom: 14px

}


textarea {

  width: 100%;

  min-height: 100px;

}

<div id="warning">The output is on the console</div>


<textarea id="subtitle">1

00:00:00,000 --> 00:00:00,600

Hi my friends


2

00:00:00,610 --> 00:00:01,050

In the first line, everything works properly

But there is a problem in the second line that I could not solve :(


3

00:00:01,080 --> 00:00:03,080

But then everything is in order and good


4

00:00:03,280 --> 00:00:05,280

You do me a great favor by helping me. Thankful</textarea>


查看完整回答
反對 回復 2023-10-14
  • 1 回答
  • 0 關注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號