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 OverflowVideos
How I would do this:
Copy// function you can use:
function getSecondPart(str) {
return str.split('-')[1];
}
// use the function:
alert(getSecondPart("sometext-20202"));
A solution I prefer would be:
Copyconst str = 'sometext-20202';
const slug = str.split('-').pop();
Where slug would be your result
You can use String.slice with String.lastIndexOf:
var str = 'test/category/1';
str.slice(0, str.lastIndexOf('/') + 1);
// => "test/category/"
str.slice(str.lastIndexOf('/') + 1);
// => 1
The actual code will depend on whether you need the full prefix or the last slash. For the last slash only, see Pedro's answer. For the full prefix (and a variable PREFIX):
var PREFIX = "test/category/";
str.substr(str.lastIndexOf(PREFIX) + PREFIX.length);
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/indexOfsubstr(): https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/substrString.length: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length
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"
const streetAddress = addy.substring(0, addy.indexOf(","));
While it’s not the best place for definitive information on what each method does (MDN Web Docs are better for that) W3Schools.com is good for introducing you to syntax.
var streetaddress = addy.split(',')[0];