开发者

How to get the last segment with regular expression?

开发者 https://www.devze.com 2022-12-14 00:10 出处:网络
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.

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_'+i开发者_开发百科d;

Could anyone tell me how to code this?


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

精彩评论

暂无评论...
验证码 换一张
取 消