How to Convert SRT to Text Using Regex in JavaScript?

As video content services such as movies, TV shows, and online videos are gaining traction these days, so is the need for different file formats such as SRT. As a developer, you can use it to your advantage and generate live captions or subtitles on the go.

With that said, today we will dive deep into what SRT files are, their uses, and how you can convert SRT to text regex JavaScript. You will also familiarize yourself with regular expressions for several development purposes. So, let’s get into it!

Understanding SRT files

SRT is an acronym for SubRip Subtitle file, a popular file format that is widely used for storing information regarding subtitle information. Using it, one can look into each subtitle’s start and end time and learn metadata and language information. Structure-wise, the SRT file is as simple as it gets. Each frame is divided into three parts: the subtitle number, start and end times, and overall text. Here is a basic rundown of how it will look:

00:00:00,000 --> 00:00:05,000
In a world...
00:00:05,000 --> 00:00:10,000
where technology reigns supreme...
00:00:10,000 --> 00:00:15,000
one developer must rise above the rest...

As for information like subtitle format or language, it is usually represented in terms of square brackets.

Understanding Regular Expressions

Moving on, a regular expression or regex is a pattern character that you can use to match or alter text strings. These characters are common in programming or web development to replace and validate text data on a large scale. Some of the widely used regex characters are under:

  • .” matches any single character except for the newline
  • *” matches zero or more occurrences of the preceding character or group
  • +” matches one or more occurrences of the preceding character or group
  • \d” matches any digit (0-9)
  • \w” matches any word character (a-z, A-Z, 0-9, and underscore)
  • \s” matches any whitespace character (space, tab, newline)

It’s worth understanding that sometimes regex include special characters such as anchors that can indicate a line or a word’s start (^) or end ($). Now why are regular impressions so crucial for SRT file conversions?

Well, for once, they can extract relevant subtitles while filtering out other information, such as subtitle numbers and times. This is important if you want to convert SRT to text regex JavaScript.

Converting SRT to Text Using Regex in JavaScript

1)   Set up the environment

First and foremost, you need to set up your development environment. You can use any preferred editor, such as Visual Studio Code, but ensure Node.js is installed on your machine.  

2)   Read SRT files using JavaScript

Next, read the SRT file through JavaScript using the “fs” module (Node.js). Here is a basic representation of how it’s done:

const fs = require('fs');

const srtFile = fs.readFileSync('', 'utf-8');

In this particular example, you’re using the “readFileSync” method to store the file’s contents.

3)   Create a Regular Expression to extract text

Once that’s finished, create a regular expression that will extract the subtitle text from the SRT file. It’s worth understanding that we will use capturing groups to extract the subtitle text. Eventually, the first capturing group should match the start and end points of the particular subtitles, while the second one to the subtitle text itself.

const plainText = extractedText.replace(/[\n\r]/g, ' ').trim();

4)   Save the plain text to a file

Finally, you need to save the plain text to a different file. For this purpose, use the “fs” module in Node.js.

fs.writeFileSync('example.txt', plainText);

This is one of the ideal ways to convert SRT to text regex JavaScript.

How Can You Handle Edge Cases?

To convert SRT files to plain text using regular expressions, you need to ensure that the resulting text is accurate and complete. Here are some common ways how you can handle these edge cases:

1)   Special Characters

While working with SRT files, you will often have to deal with several special characters, such as accent marks or normal symbols. The ideal way to deal with them is through the “u” flag, which will enable Unicode support in the end. Your regular expression will convert in the following way:

const regex = /[\d]+\n([\d:,]+) --> [\d:,]+\n(.+)\n/gu;

2)   Empty Lines

Now, SRT files can also contain empty lines between subtitles, which will ultimately cause extraction issues. You can overcome this by using the “[\n\n\r]+” pattern which will modify regular expression as:

const regex = /[\d]+\n([\d:,]+) --> [\d:,]+\n(.+)[\n\n\r]+/gu;

3)   Multiple Languages

SRT files can be found in a variety of languages, and it’s not always easy to convert them by basic regular expressions. This is because the text for each language needs to be correctly identified and separated.

One of the popular approaches to do this is to create regular expressions for that particular language and sequence them manually. Similarly, the next way is to modify the regular expression itself and include language identifiers ([ENG] or [SPA]).

const regex = /[\d]+\n([\d:,]+) --> [\d:,]+\n\[[A-Z]{3}\](.+)[\n\n\r]+/gu;

Here “\[A-Z]{3}\]” can identify the language of the subtitle.

Advantages Of Converting SRT to Text Using Regex JavaScript

1)   Accessibility for hearing-impaired individuals

Converting SRT files to plain text allows hearing-impaired individuals to engage more with the video content. By allowing closed captions, you can make your content more inclusive and better tailored for the audience.

2)   Easier Editing & Content Sharing

SRT files also allow you to edit the plain text with better clarity. You can use any editor, such as MS Word or Google Docs, and bring changes on the go. Alternatively, this also makes sharing subtitle content across multiple apps much easier without the need for any specialized software.

3)   Better search engine optimization

SRT file conversions can provide you with search engine optimization (SEO) for video content. By using plain text extracted from SRT files, you can index your video content and expand your reach.  


All in all, this was a brief run-down on how you can convert SRT to text using Regex in JavaScript. Of course, you can also use machine learning to increase the accuracy of the whole process, but it comes with its own set of caveats. In the end, SRT file conversion is a viable tool that can make your content easier to edit and allows for better search engine optimization.


Q1, What programming languages can be used for SRT to Text conversion?

This process is supported by many different programming languages, including Python, Ruby, and JavaScript.

Q2, How accurate is the SRT to Text conversion process?

Well, the accuracy of this process depends on the complexity of the SRT file and the type of regular expression used. Simple regular expressions, in general, won’t be able to many edge cases such as special characters and vice versa.

Q3, Can this process be applied to other file formats?

The techniques provided earlier are specific to SRT files. However, there are regular expressions that work with SubRip (SUB) or WebVTT (VTT).

Scroll to Top