2009-12-11 5 views
1

J'ai l'URL suivante.Comment obtenir le dernier segment avec une expression régulière?

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

Je veux obtenir 24 de ce à utiliser dans jquery/javascript.

Quelque chose comme ça.

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

Quelqu'un pourrait-il me dire comment le coder?

+0

Voir http://stackoverflow.com/questions/1788908/what-is-the-best-way-to-cut-the-file-name-from-href-attribute -of-an-a-element/1789528 # 1789528 (aussi, ceci est une copie de cette question). –

Répondre

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

this.id = 'delete_link_' + id; 
4

Pourquoi utiliser regex?

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

Regex est exagéré ici.

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