How can I cut a substring from a string to the end

2020-02-26 07:25发布

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

7条回答
劫难
2楼-- · 2020-02-26 07:58

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]);​
查看更多
叼着烟拽天下
3楼-- · 2020-02-26 08:06

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"
查看更多
Summer. ? 凉城
4楼-- · 2020-02-26 08:10

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);

查看更多
劫难
5楼-- · 2020-02-26 08:11

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 -

查看更多
啃猪蹄的小仙女
6楼-- · 2020-02-26 08:19

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);
查看更多
小情绪 Triste *
7楼-- · 2020-02-26 08:22

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]);
查看更多
登录 后发表回答