-1

I use regex to create an array of subtitles at any time that includes [number, start, end, text]

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

But the problem is that in the texts section, if the number of lines is 2 or more, they will not be read.

Here you can see the relevant image

I do not want only the first line of the text in the section to be considered as text, if there are other lines, they should be considered as well.

You do me a great favor by helping me. Thankful

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">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>

1 Answers1

2

Replace /gm with /g, otherwise, the $ is referring to the end of the first "Text" line, and the Regex does not try to match anything after it:

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>
blex
  • 24,941
  • 5
  • 39
  • 72