Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I cut a substring from a string to the end using Javascript?

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
like image 285
Erick Engelhardt Avatar asked Mar 13 '12 20:03

Erick Engelhardt


People also ask

How do you slice a substring in JavaScript?

The slice() method extracts a part of a string. The slice() method returns the extracted part in a new string. The slice() method does not change the original string. The start and end parameters specifies the part of the string to extract.

How do I cut the last character of a string in JavaScript?

To remove the last character from a string in JavaScript, you should use the slice() method. It takes two arguments: the start index and the end index. slice() supports negative indexing, which means that slice(0, -1) is equivalent to slice(0, str. length - 1) .

How do substring () and substr () differ in JavaScript?

The difference between substring() and substr()The two parameters of substr() are start and length , while for substring() , they are start and end . substr() 's start index will wrap to the end of the string if it is negative, while substring() will clamp it to 0 .

How do you clip a string in JavaScript?

JavaScript String trim() The trim() is a built-in string function in JavaScript, which is used to trim a string. This function removes the whitespace from both the ends, i.e., start and end of the string. As the trim() is a string method, so it is invoked by an instance of the String class.


1 Answers

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
like image 83
Jasper Avatar answered Sep 28 '22 21:09

Jasper