EN
JavaScript - get substring after character
3
points
In this article, we would like to show you how to get substring after character using JavaScript.
Quick solution:
// ONLINE-RUNNER:browser;
const character = ':';
const string = 'user:password';
const index = string.indexOf(character);
const substring = string.substring(index + 1);
console.log(substring); // password
Practical example
In this example, we create a reusable function that helps to get the substring after indicated character.
// ONLINE-RUNNER:browser;
const getSubstring = (string, character) => {
const index = string.indexOf(character);
return string.substring(index + 1);
};
// usage example:
const text1 = 'Get the value:value1';
const text2 = 'Get the value:value2';
const result1 = getSubstring(text1, ':'); // value1
const result2 = getSubstring(text2, ':'); // value2
console.log(result1); // value1
console.log(result2); // value2