20

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
6
  • Hi Erick, the link you shared is on your local machine so is not visible. If you rephrase your question with a simple code snippet of what you currently have, then I can attempt to answer Commented Mar 13, 2012 at 20:15
  • Will your substring always start /dashboard...? Edit: @Rowan, the link is just an example of the string he's trying to cut the substring from. I hope. Commented Mar 13, 2012 at 20:15
  • Haha, oops. Fail :) Listen to @Elliot – confused me as it appears as a link, natural reaction is to click Commented Mar 13, 2012 at 20:16
  • Hello. Thanks for yout reply. Makes no difference the server. I want to cut after the last "/". Commented Mar 13, 2012 at 20:17
  • JavaScript substr will do for this, no jQuery needed. Commented Mar 13, 2012 at 20:24

7 Answers 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 -

Sign up to request clarification or add additional context in comments.

3 Comments

I see many vote up, But this have two problem: 1) this included / char, but in the question, refer to without /. 2) for resolve that, if use indexOf...+1 then return all string when there are not any /dashboard... , so I suggestion use this solution: stackoverflow.com/a/58456746/1407491
@NabiK.A.Z. Good catch. Answer updated to reflect the slash being included in output.
Of course remember to use subString instead of deprecated subStr.
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"

Comments

5

If the beginning is always "http://localhost/40ATV" you can do this:

var a = "http://localhost/40ATV/dashboard.php?page_id=projeto_lista&lista_tipo=equipe";
var cut = a.substr(22);

3 Comments

It may help to explain how something works at times instead of just answering the question with a utilitarian answer. substr/substring and indexOf are all important operations that can/should be covered.
Thanks for the input. I'm new around here so please bare with me :)
Not a problem.. just offering advice for more constructive answers. :)
4

The native JavaScript String method substr[MDN] can accomplish what you need. Just supply the starting index and omit the length parameter, and it grabs all the way to the end.

Now, how to go about getting the starting index? You didn't give any criteria, so I can't really help with that.

2 Comments

I don't think is so easy. substr cannot find a specific group of characteres.
Yes. For that, you would need to find the starting index.
2

No need for jQuery, plain old javascript will do the job just fine.

var myString = "http://localhost/40ATV/dashboard.php?page_id=projeto_lista&lista_tipo=equipe";
var mySplitResult = myString.split("\/");
document.write(mySplitResult[mySplitResult.length - 1]);​

and if you want the leading /

document.write("/" + mySplitResult[mySplitResult.length - 1]);​

Comments

0

First of all SPLIT URL:

var str = "http://localhost/40ATV/dashboard.php?page_id=projeto_lista&lista_tipo=equipe";
var arr_split = str.split("/");

Find the last array:

var num = arr_split.length-1;

You get the address after the last dash:

alert(arr_split[num]);

Comments

0

You can use this code, if there are not dashboard... return empty.

var str = 'http://localhost/40ATV/dashboard.php?page_id=projeto_lista&lista_tipo=equipe';
var index = str.indexOf('/dashboard.php') + 1;
var result = '';
if (index != 0) {
  result = str.substr(index);
}
console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.