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

javascript - How to get the last segment with regular expression? - Stack Overflow

programmeradmin4浏览0评论

I have the following url.

http://127.0.0.1/ci/index.php/admin/menus/edit/24

I want to get 24 from this to use in jquery/javascript.

Something like this.

var id=this.href.replace(/.*=/,'');
this.id='delete_link_'+id;

Could anyone tell me how to code this?

I have the following url.

http://127.0.0.1/ci/index.php/admin/menus/edit/24

I want to get 24 from this to use in jquery/javascript.

Something like this.

var id=this.href.replace(/.*=/,'');
this.id='delete_link_'+id;

Could anyone tell me how to code this?

Share Improve this question asked Dec 11, 2009 at 20:23 shinshin 32.7k71 gold badges193 silver badges272 bronze badges 1
  • See stackoverflow./questions/1788908/… (also, this is a duplicate of that one question). – Crescent Fresh Commented Dec 11, 2009 at 21:04
Add a ment  | 

4 Answers 4

Reset to default 5
var id = this.href.match(/[^\/]*$/)

this.id = 'delete_link_' + id;

Why use regex?

var parts=this.href.split("/");
var id = parts[parts.length - 1];
this.id='delete_link_'+id;

Regex is overkill here.

var s = "http://127.0.0.1/ci/index.php/admin/menus/edit/24";
s.substring(s.lastIndexOf("/")+1);
"http://127.0.0.1/ci/index.php/admin/menus/edit/24".match(/^.*\/([^\/]+)$/)[1]
发布评论

评论列表(0)

  1. 暂无评论