简体   繁体   中英

How to get the last segment with regular expression?

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?

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM