How I would do this:

Copy// function you can use:
function getSecondPart(str) {
    return str.split('-')[1];
}
// use the function:
alert(getSecondPart("sometext-20202"));
Answer from artlung on Stack Overflow
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-get-substring-after-specific-character
Get the Substring after a specific Character in JavaScript | bobbyhadz
Alternatively, you can use the str.split() method. ... Use the split() method to split the string on the character. Access the array of strings at index 1. The first element in the array is the substring after the character.
🌐
W3Schools
w3schools.com › jsref › jsref_substring.asp
JavaScript String substring() Method
The substring() method extracts characters from start to end (exclusive).
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › substring
String.prototype.substring() - JavaScript | MDN
The substring() method of String values returns the part of this string from the start index up to and excluding the end index, or to the end of the string if no end index is supplied.
🌐
tutorialstonight
tutorialstonight.com › javascript-substring-after-character
Find JavaScript Substring After Character (3 Ways)
This way we can get the substring after the dash. The replace() method in JavaScript replaces a substring with another substring. The idea here is to replace all the characters in the string before the given character with ...
🌐
TutorialsPoint
tutorialspoint.com › how-to-get-a-part-of-string-after-a-specified-character-in-javascript
How to get a part of string after a specified character in JavaScript?
... <html> <body> <script> function subStr(string, character, position) { if(position=='b') return string.substring(string.indexOf(character) + 1); else if(position=='a') return string.substring(0, string.indexOf(character)); else return string; } document.write(subStr('Tutorix & ...
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-string-exercise-22.php
JavaScript validation with regular expression: Get a part of string after a specified character - w3resource
Write a JavaScript function to get a part of string after a specified character. Test Data: console.log(subStrAfterChars('w3resource: JavaScript Exercises', ':','a')); console.log(subStrAfterChars('w3resource: JavaScript Exercises', 'E','b')); Output: "w3resource" "xercises"
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › substr
String.prototype.substr() - JavaScript | MDN
The substr() method of String values returns a portion of this string, starting at the specified index and extending for a given number of characters afterwards.
Find elsewhere
🌐
Futurestud.io
futurestud.io › tutorials › get-the-part-after-a-character-in-a-string-in-javascript-or-node-js
Get the Part After a Character in a String in JavaScript or Node.js
function strAfter (string, delimiter) { if (delimiter === '') { return string } const substrings = string.split(delimiter) return substrings.length === 1 ? string // delimiter is not part of the string : substrings.slice(1).join(delimiter) } // Using strAfter const range = 'A1:L200' const end = strAfter(range, ':') // L200 const slug = '2020-this-is-a-tutorial-slug') const scrambledSlug = strAfter(slug, '-') // 'this-is-a-tutorial-slug' The check if the given delimiter is empty is important. JavaScript splits a given string value at each character when using an empty string as the delimiter.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-remove-portion-of-a-string-after-certain-character-in-javascript
How to remove portion of a string after certain character in JavaScript ? - GeeksforGeeks
February 15, 2023 - It specifies the integer that specifies ... JavaScript String substring() Method: This method gets the characters from a string, between two defined indices, and returns the new sub string....
🌐
Futurestud.io
futurestud.io › tutorials › get-the-part-after-first-occurrence-in-a-string-in-javascript-or-node-js
Get the Part After First Occurrence in a String in JavaScript or Node.js
The after utility method should return the remainder of a string after matching the occurrence of a character sequence. That means you want to return everything from the array of substrings except the first item.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-remove-everything-after-specific-character-in-string
Remove everything after a specific Character in JavaScript | bobbyhadz
The split() method returned an array of 2 substrings - the parts of the string before and after the character. ... Copied!const str = 'BMW[1996]'; const split = str.split('['); console.log(split); // 👉️ [ 'BMW', '1996]' ] console.log(split[0]); ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-get-value-of-a-string-after-last-slash-in-javascript
How to get value of a string after last slash in JavaScript? - GeeksforGeeks
July 12, 2025 - ... let str = "folder_1/folder... the last index of ('/') using .lastIndexOf(str) method. Use the .substring() method to get access the string after the last slash....
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-get-string-after-last-slash
Get the Part after Last Occurrence in a String in JavaScript | bobbyhadz
We called the Array.pop() method to remove and return the last element from the array. ... Copied!const str = 'hello/world/index.html'; const afterLastSlash = str.split('/').pop(); console.log(afterLastSlash); // 👉️ index.html · If you care about performance you would use the 1st approach, even though the difference is negligible unless you work with very large strings. You can learn more about the related topics by checking out the following tutorials: Replace Last Occurrence of Character in String in JavaScript
🌐
Refine
refine.dev › home › blog › tutorials › javascript substring method
JavaScript Substring Method | Refine
December 31, 2024 - Array.prototype.substring() Method Signature · JavaScript substring() with startIndex Only · Extract Tail After First n Characters - JavaScript substring() JavaScript String.prototype.substring() - Extract a Substring Between Two Points ·
🌐
W3Schools
w3schools.com › jsref › jsref_substr.asp
JavaScript String substr() Method
The substr() method begins at a specified position, and returns a specified number of characters.
Top answer
1 of 12
385

At least three ways:

A regular expression:

var result = /[^/]*$/.exec("foo/bar/test.html")[0];

...which says "grab the series of characters not containing a slash" ([^/]*) at the end of the string ($). Then it grabs the matched characters from the returned match object by indexing into it ([0]); in a match object, the first entry is the whole matched string. No need for capture groups.

Live example

Using lastIndexOf and substring:

var str = "foo/bar/test.html";
var n = str.lastIndexOf('/');
var result = str.substring(n + 1);

lastIndexOf does what it sounds like it does: It finds the index of the last occurrence of a character (well, string) in a string, returning -1 if not found. Nine times out of ten you probably want to check that return value (if (n !== -1)), but in the above since we're adding 1 to it and calling substring, we'd end up doing str.substring(0) which just returns the string.

Using Array#split

Sudhir and Tom Walters have this covered here and here, but just for completeness:

var parts = "foo/bar/test.html".split("/");
var result = parts[parts.length - 1]; // Or parts.pop();

split splits up a string using the given delimiter, returning an array.

The lastIndexOf / substring solution is probably the most efficient (although one always has to be careful saying anything about JavaScript and performance, since the engines vary so radically from each other), but unless you're doing this thousands of times in a loop, it doesn't matter and I'd strive for clarity of code.

2 of 12
83

Try this:

const url = "files/images/gallery/image.jpg";

console.log(url.split("/").pop());