How to Convert SRT to Text with Regex and Javascript

Looking for a way to convert SRT files and texts (subtitles for movies, series and anime) into text using regex and javascript? This article will help you with this and other alternatives.

How to Convert SRT to Text with Javascript and Regex

To convert an SRT (SubRip Text) subtitle file to text using regex in JavaScript, you can use the following function:

function convertSrtToText(srt) {
  // Use a expressão regular para remover os números de linha e as marcas de tempo
  return srt.replace(/^\d+\n([\d:,]+ --> [\d:,]+\n)/gm, '');
}

This function uses a regular expression to remove line numbers and timestamps from the SRT file. It returns the remaining text of the SRT file, minus the line numbers and timestamps.

To use this function, just call it passing the SRT file content as a parameter, as in the following example:

var srt = "1\n00:00:10,500 --> 00:00:13,000\nTexto da linha 1\n\n2\n00:00:13,500 --> 00:00:16,000\nTexto da linha 2\n\n3\n00:00:16,500 --> 00:00:19,000\nTexto da linha 3\n";
var text = convertSrtToText(srt);
console.log(text); // Exibe "Texto da linha 1\n\nTexto da linha 2\n\nTexto da linha 3\n"

Convert SRT using JS Modules

There are other alternatives to convert an SRT file to text using JavaScript. Here are some options you can consider:

srt-to-vtt module

  • Using the srt-to-vtt module: The srt-to-vtt module is an npm package that can be used to convert SRT files to text. To use it, you need to install it with the command:
  • npm install srt-to-vtt
  • Then use the following code:
const srtToVtt = require('srt-to-vtt');

srtToVtt.convertSrtToVtt('path/to/input.srt', 'path/to/output.vtt', (err) => {
  if (err) {
    console.error(err);
  } else {
    console.log('Conversão concluída com sucesso');
  }
});

srt-to-txt module

Using the srt-to-txt module: The srt-to-txt module is another npm package that can be used to convert SRT files to text. To use it, you need to install it with the command:

  • npm install srt-to-txt
const srtToTxt = require('srt-to-txt');

srtToTxt('path/to/input.srt').then((text) => {
  console.log(text);
});

SubRip-Text library

Using the SubRip-Text library: The SubRip-Text library is a JavaScript library that can be used to read and manipulate SRT files. To use it, you need to install it with the command:

  • npm install subrip-text
const SubRipText = require('subrip-text');

const srt = new SubRipText('path/to/input.srt');

console.log(srt.getPlainText());

Other ways to convert SRT to TXT

There are other ways to convert an SRT file to text (TXT). Some options you can consider are:

  1. Using an online converter: there are several online converters that allow you to convert SRT files to text. Just upload the SRT file and the converter will do the conversion for you.
  2. Use a text editor: many text editors such as Notepad++ or Sublime Text have options for removing line numbers and timestamps from SRT files. You can use these options to remove these elements and save the file as a regular text file.
  3. Using a command line script: if you are working with large amounts of SRT files and need to do the conversion in an automated way, it can be useful to use a command line script like the ones in this article.