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
🌐
W3Schools
w3schools.com › jsref › jsref_substring.asp
JavaScript String substring() Method
More examples below. The substring() method extracts characters, between two indices (positions), from a string, and returns the substring.
🌐
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.
🌐
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
// Define a function named ... (before), return the substring after the specified character. if(pos=='b') return str.substring(str.indexOf(char) + 1); // If the position indicator is 'a' (after), return the substring ...
🌐
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 & ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › substr
String.prototype.substr() - JavaScript | MDN
const string = "Mozilla"; console.log(string.substr(0, 1)); // 'M' console.log(string.substr(1, 0)); // '' console.log(string.substr(-1, 1)); // 'a' console.log(string.substr(1, -1)); // '' console.log(string.substr(-3)); // 'lla' console.log(string.substr(1)); // 'ozilla' console.log(string.substr(-20, 2)); // 'Mo' console.log(string.substr(20, 2)); // ''
🌐
W3Schools
w3schools.com › jsref › jsref_substr.asp
JavaScript String substr() Method
More examples below. The substr() method extracts a part of a string. The substr() method begins at a specified position, and returns a specified number of characters. The substr() method does not change the original string. To extract characters from the end of the string, use a negative start position. The substr() method is removed (deprecated) in the latest JavaScript standard.
Find elsewhere
🌐
Refine
refine.dev › home › blog › tutorials › javascript substring method
JavaScript Substring Method | Refine
December 31, 2024 - While doing so, we elaborate on a few patterns of extraction from a parent string: namely, that of extracting a tail after first n characters, another of producing a substring composed of first n characters and that of extracting last n characters. In the later half of the post, we discuss some of the nuances associated with startIndex and endIndex values. In the end, we point out how JavaScript String.prototype.substring() compares to String.prototype.slice() as well as the deprecated method String.prototype.substr().
🌐
Tutorial Reference
tutorialreference.com › javascript › examples › faq › javascript-how-to-get-the-substring-after-the-last-occurrence-of-a-character
How to Get the Substring After the Last Occurrence of a Character in JavaScript | Tutorial Reference
Use String.prototype.split(char) to break the string into an array of substrings, using the character as the delimiter. Use Array.prototype.pop() to remove and return the last element of that array. ... function getAfterLast(str, char) { return str.split(char).pop(); } // Example Usage: const ...
🌐
IQCode
iqcode.com › code › javascript › javascript-substring-after-character
javascript substring after character Code Example
const str = '01-01-2020'; // get everything after first dash const slug = str.substring(str.indexOf('-') + 1); // 01-2020 // get everything after last dash const slug = str.split('-').pop(); // 2020 ... var testStr = &quot;sometext-20202&quot; var splitStr = testStr.substring(testStr.indexOf('-') ...
🌐
Sabe
sabe.io › blog › javascript-get-substring-after-specific-character
Get the Substring after a Specific Character in JavaScript
May 10, 2022 - JAVASCRIPTconst input = "1) Hello World"; const index = input.indexOf(")"); const substring = input.substring(index + 1); console.log(substring); ... We want to increment the index by one so we exclude the character itself.
🌐
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
... function getAfterFirst(str, ... to the end return str.slice(firstIndex + 1); } // Example Usage: const data = 'user=Alice&role=admin'; const value = getAfterFirst(data, '='); console.log(value);...
🌐
ReqBin
reqbin.com › code › javascript › i5foejaa › javascript-substring-example
How do I get a substring from a string in JavaScript?
Getting Substring from String in JavaScript Execute · let str = 'JavaScript Substring'; console.log(str.substring(4, 10)); ... JavaScript strings are sequences of characters, including letters, numbers, and symbols.
Top answer
1 of 7
43

You can use indexOf and substr to get the sub-string you want:

//using a string variable set to the URL you want to pull info from
//this could be set to `window.location.href` instead to get the current URL
var strIn  = 'http://localhost/40ATV/dashboard.php?page_id=projeto_lista&lista_tipo=equipe',

    //get the index of the start of the part of the URL we want to keep
    index  = strIn.indexOf('/dashboard.php'),

    //then get everything after the found index
    strOut = strIn.substr(index);

The strOut variable now holds everything after /dashboard.php (including that string).

Here is a demo: http://jsfiddle.net/DupwQ/

UPDATE:

The strOut variable in the example above includes the prefixed forward slash and it was requested that the output should not.

Replacing strOut = strIn.substr(index) with strOut = strIn.substr(index + 1) fixes the output for this specific use case by starting the substring one character farther ahead in the string.

Something else you could do is search for the string after a specific search term (non-inclusive):

var strIn = 'http://localhost/40ATV/dashboard.php?page_id=projeto_lista&lista_tipo=equipe';
var searchTerm = '/dashboard.php?';
var searchIndex = strIn.indexOf(searchTerm);
var strOut = strIn.substr(searchIndex + searchTerm.length); //this is where the magic happens :)

strOut now holds everything after /dashboard.php? (non-inclusive).

Here is an updated demo: http://jsfiddle.net/7ud0pnmr/1/

Docs -

  • indexOf(): https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/indexOf
  • substr(): https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/substr
  • String.length: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length
2 of 7
9

This may be new, but the substring method returns everything from a specified index to the end of the string.

var string = "This is a test";

console.log(string.substring(5));
// returns "is a test"
🌐
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
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-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_2/file.html"; function GFG_FUN() { console.log(str.substring(str.lastIndexOf('/') + 1)); } GFG_FUN(); ... Example: This example uses the above approach.
🌐
Upmostly
upmostly.com › home › tutorials › javascript substring: extracting strings (with examples)
How to Use the Substring Method in Javascript (Code Examples)
October 28, 2021 - Beginning from 0 (as all indexes in JavaScript do), we count 5 characters from left to right of name. Substring selects the characters between the specified index arguments. The end index value is not included in the returned string from substring. Therefore, “dog”.substring(0,1) will return only ‘d’.