Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx - Get All Characters After Last Slash in URL

I'm working with a Google API that returns IDs in the below format, which I've saved as a string. How can I write a Regular Expression in javascript to trim the string to only the characters after the last slash in the URL.

var id = 'http://www.google.com/m8/feeds/contacts/myemail%40gmail.com/base/nabb80191e23b7d9' 
like image 379
ac360 Avatar asked Nov 04 '13 20:11

ac360


1 Answers

Don't write a regex! This is trivial to do with string functions instead:

var final = id.substr(id.lastIndexOf('/') + 1); 

It's even easier if you know that the final part will always be 16 characters:

var final = id.substr(-16); 
like image 88
lonesomeday Avatar answered Sep 21 '22 21:09

lonesomeday