To get the substring between two specific characters in JavaScript, you can use a combination of the `substring` method and the `indexOf` method to extract the substring.
In this example,
url
with a value of "https://example.com/page?query=1"
.indexOf
method to find the indices of '/'
and '?'
.substring
method with the indices to extract the substring between the characters.let url = 'https://example.com/page?query=1';
let start = url.indexOf('/') + 1;
let end = url.indexOf('?');
let substring = url.substring(start, end);
console.log('Substring between / and ? : ' + substring);
Substring between / and ? : /example.com/page
In this example,
str
with a value of "Hello,World!"
.indexOf
method to find the indices of ','
and '!'
.substring
method with the indices to extract the substring between the characters.let str = 'Hello,World!';
let start = str.indexOf(',') + 1;
let end = str.indexOf('!');
let substring = str.substring(start, end);
console.log('Substring between , and ! : ' + substring);
Substring between , and ! : World
In this tutorial, we learned How to get Substring between two Specific Characters in JavaScript language with well detailed examples.