How I would do this:

// function you can use:
function getSecondPart(str) {
    return str.split('-')[1];
}
// use the function:
alert(getSecondPart("sometext-20202"));
Answer from artlung on Stack Overflow
🌐
W3Schools
w3schools.com › jsref › jsref_substring.asp
JavaScript String substring() Method
The substring() method extracts characters from start to end (exclusive).
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-get-substring-after-specific-character
Get the Substring after a specific Character in JavaScript | bobbyhadz
Use the String.indexOf() method to get the index of the character in the string. Use the String.slice() method to get the substring after the specific character.
Discussions

How to get substring after last specific character in JavaScript? - Stack Overflow
I have a string test/category/1. I have to get substring after test/category/. How can I do that? More on stackoverflow.com
🌐 stackoverflow.com
May 23, 2017
substring - Get value of a string after last slash in JavaScript - Stack Overflow
I am already trying for over an hour and cant figure out the right way to do it, although it is probably pretty easy: I have something like this : foo/bar/test.html I would like to use jQuery to More on stackoverflow.com
🌐 stackoverflow.com
Removing rest of string after a certain character
gsub("\\(.*", "", my_string) should work. \\( matches the '(' character (the backslashes are to escape it, since '(' is a special character in regular expressions), .* matches everything after it. More on reddit.com
🌐 r/rstats
4
3
June 3, 2021
Truncate String after second period
-split is probably the quickest (grab split 0 and split 1) $A = "5.10.123.753" $A.split('.')[0..1] -join '.' 5.10 regex is probably the cleanest (match digits followed by a .) $A -match "\b(?:\d{1,3}\.)(?:\d{1,3})" $A -match "\b(?:\d{1,3}\.){2}" $A -match "\d+\.\d+" $Matches.Values 5.10 casing to a version is good too (grab major and minor build) [version]$A = "5.10.123.753" Major Minor Build Revision ----- ----- ----- -------- 5 10 123 753 "$($a.Major).$($a.Minor)" 5.10 More on reddit.com
🌐 r/PowerShell
35
37
August 7, 2022
🌐
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
July 17, 2025 - If 'pos' is 'b' (before), it returns the substring after the specified character using 'substring' and 'indexOf' functions.
🌐
TutorialsPoint
tutorialspoint.com › article › 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?
March 15, 2026 - To get a part of a string after a specified character in JavaScript, you can use the substring() method combined with indexOf(). This technique allows you to extract portions of text before or after any character.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › substring
String.prototype.substring() - JavaScript - MDN Web Docs
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.
🌐
Sabe
sabe.io › blog › javascript-get-substring-after-specific-character
Get the Substring after a Specific Character in JavaScript | Sabe
May 10, 2022 - To get this, we can use the built-in string method indexOf(). This method takes a character and returns the index of that character, which is the number of characters before the character.
🌐
Refine
refine.dev › home › blog › tutorials › javascript substring method
JavaScript Substring Method | Refine
January 1, 2025 - We also figured out how we can extract the last n characters using caller length - n. Towards the later half, we touch based on some other nuances of using startIndex and endIndex values. In the end, we compared and discussed how substring() differs in implementation from JavaScript slice() and substr(). ... IntroductionWhat is JavaScript substring()?JavaScript substring() MethodArray.prototype.substring() Method SignatureJavaScript substring() with startIndex OnlyExtract Tail After First n Characters - JavaScript substring()JavaScript String.prototype.substring() - Extract a Substring Between
Find elsewhere
🌐
Tutorial Reference
tutorialreference.com › javascript › examples › faq › javascript-how-to-get-substring-after-a-character
How to Get the Substring After a Character in JavaScript | Tutorial Reference
This is the most performant, robust, and readable method for this specific task. It avoids creating unnecessary intermediate arrays. ... Use String.prototype.indexOf(char) to find the index of the first occurrence of the delimiter. Add 1 to this index to get the starting position of the substring ...
🌐
GeeksforGeeks
geeksforgeeks.org › 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 - To replace a character from a string there are popular methods available, the two most popular methods we are going to describe in this article. The first method is by using the substr() method.
🌐
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
October 29, 2020 - string // delimiter is not part ...utorial-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....
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
84

Try this:

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

console.log(url.split("/").pop());
Run code snippetEdit code snippet Hide Results Copy to answer Expand

🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-get-string-after-last-slash
Get the Part after Last Occurrence in a String in JavaScript | bobbyhadz
Use the String.pop() method to get the part of the string after the last occurrence. ... Copied!const str = 'hello/world/index.html'; const afterLastSlash = str.split('/').pop(); console.log(afterLastSlash); // 👉️ index.html ...
🌐
JavaScript in Plain English
javascript.plainenglish.io › how-to-remove-everything-after-a-certain-character-df59c805ba40
How to Remove Everything After a Certain Character in a JavaScript String | by John Au-Yeung | JavaScript in Plain English
December 19, 2022 - Therefore, path is: '/Controller/Action’ . The indexOf method lets us get the index of the given character in a string. We can use it with the substring method to extract the substring from the beginning to the given index returned by indexOf .
🌐
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
February 10, 2022 - When interacting with string values you may want to retrieve a portion of the string after a given character sequence. This “after” method isn’t available in JavaScript directly and we’re building it ourselves. JavaScript comes with the String#split method allowing you to split a string ...
🌐
Futurestud.io
futurestud.io › tutorials › get-the-part-after-last-occurrence-in-a-string-in-javascript-or-node-js
Get the Part After Last Occurrence in a String in JavaScript or Node.js
February 24, 2022 - That’s what this tutorial shows you: how to build your own afterLast string utility method. ... JavaScript comes with the String#split method. This split method divides a given string value at each occurrence of a given delimiter. The result is an array of ordered substrings.