How to remove last params from the url for instance, i have url such as
http://localhost/autoservice/public_html/tickets/load_service/6
i want to get this result
http://localhost/autoservice/public_html/tickets
how to make this with jquery or javascript
i have tried many solutions by reading many post on stack but i can't get it right
var url = $(location).attr('href');
How to remove last params from the url for instance, i have url such as
http://localhost/autoservice/public_html/tickets/load_service/6
i want to get this result
http://localhost/autoservice/public_html/tickets
how to make this with jquery or javascript
i have tried many solutions by reading many post on stack but i can't get it right
var url = $(location).attr('href');
Share
Improve this question
asked Feb 4, 2015 at 9:14
Share KnowledgeShare Knowledge
2973 silver badges18 bronze badges
1
-
4
What is the rule to decide which are the "last parameters"? Remove the last 2 elements inside
/
? Everything after a given string i.e. "tickets"? – Giorgio Commented Feb 4, 2015 at 9:18
2 Answers
Reset to default 13Try this simple method using lastIndexOf()
and slice()
url = 'http://localhost/autoservice/public_html/tickets/load_service/6';
url = url.slice(0, url.lastIndexOf('/'));
url = url.slice(0, url.lastIndexOf('/'));
alert(url);
You can use regex here:
^(.+)(\/[^\/]+\/.+)$
DEMO
Used with Javascript:
var url = 'http://localhost/autoservice/public_html/tickets/load_service/6';
alert(
'BEFORE\n' + url + '\n\n'+
'AFTER\n' + url.replace(/^(.+)(\/[^\/]+\/.+)$/g, '$1')
);