最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove last params from url - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 13

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

发布评论

评论列表(0)

  1. 暂无评论