To get the substring before a specific character in JavaScript, you can use the `substring` method along with the `indexOf` method to find the position of the character.
In this example,
url
with a value of "https://example.com/page"
.substring
method with the starting index of 0 and the index of '/'
to get the substring before the specified character.let url = 'https://example.com/page';
let substring = url.substring(0, url.indexOf('/'));
console.log('Substring before / : ' + substring);
Substring before / : https:
In this example,
str
with a value of "Hello,World"
.substring
method with the starting index of 0 and the index of ','
to get the substring before the specified character.let str = 'Hello,World';
let substring = str.substring(0, str.indexOf(','));
console.log('Substring before , : ' + substring);
Substring before , : Hello
In this tutorial, we learned How to get Substring before Specific Character in JavaScript language with well detailed examples.