I have a url:
http://localhost/40ATV/dashboard.php?page_id=projeto_lista&lista_tipo=equipe
I want to get the address after the last dash using javascript:
dashboard.php?page_id=projeto_lista&lista_tipo=equipe
I have a url:
http://localhost/40ATV/dashboard.php?page_id=projeto_lista&lista_tipo=equipe
I want to get the address after the last dash using javascript:
dashboard.php?page_id=projeto_lista&lista_tipo=equipe
No need for jQuery, plain old javascript will do the job just fine.
and if you want the leading /
This may be new, but the substring method returns everything from a specified index to the end of the string.
You can use this code, if there are not
dashboard...
return empty.You can use
indexOf
andsubstr
to get the sub-string you want: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)
withstrOut = 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):
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/lengthIf the beginning is always "http://localhost/40ATV" you can do this:
First of all SPLIT URL:
Find the last array:
You get the address after the last dash: