Computer >> Computer tutorials >  >> Programming >> Javascript

How to get substring between two similar characters in JavaScript?


Let’s say we have the following string with some colons −

var values="This is:the first program in JavaScript:After that I will do Data Structure program";

We need to get the substring between the colon. The output should be −

the first program in JavaScript

For this, use the concept of split().

Example

Following is the code −

var values="This is:the first program in JavaScript:After that I will do Data Structure program";
var result=[];
result=values.split(":");
console.log(result[1]);

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo273.js.

Output

This will produce the following output on console −

PS C:\Users\Amit\javascript-code> node demo273.js
the first program in JavaScript