JavaScript - get substring between two characters

0 points
Created by:

In this article, we would like to show you how to get substring between two characters using JavaScript.

Quick solution:

text.substring(indexStart, indexEnd);


Practical example

In this example, we use indexOf() method to get the indexes of the characters between which we want to get our result substring. Then we use substring() method to get the result.

Runnable example:

// ONLINE-RUNNER:browser;

const text = 'Get the text:example_text.';

const indexStart = text.indexOf(':') + 1;
const indexEnd   = text.indexOf('.');

const result = text.substring(indexStart, indexEnd);
console.log(result);  // example_text


  1. String.prototype.indexOf() - JavaScript | MDN
  2. String.prototype.substring() - JavaScript | MDN 
Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂