60

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'
ac360
  • 7,527
  • 12
  • 49
  • 86

8 Answers8

101

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);
lonesomeday
  • 224,675
  • 49
  • 309
  • 312
  • 1
    Will not return anything is the url is `'http://www.google.com/m8/feeds/contacts/myemail%40gmail.com/base/nabb80191e23b7d9/'` – Allan Kimmer Jensen Mar 17 '15 at 15:48
  • 12
    @AllanKimmerJensen Well, there isn't anything after the last slash there, of course. – lonesomeday Mar 17 '15 at 22:58
  • 3
    I found this topic although I was looking for a PHP solution. I converted it, in case anyone is interested: `substr($id, strrpos($id, "/") + 1)` – Jan Sep 30 '15 at 14:12
  • Not an answer to the question! – LinusGeffarth Oct 27 '20 at 10:46
  • @LinusGeffarth https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – lonesomeday Oct 27 '20 at 13:24
  • Why is this so upvoted? This shows up as the first question on google when searching for a regex solution. OP specifically asked for a regex, and the answer starts with "Don't write a regex" : ( – Mercury Feb 14 '22 at 11:51
  • @Mercury Cos it's sensible advice? "How do I knock a nail into a wall with a harpsichord?" "Don't: use a hammer." – lonesomeday Feb 14 '22 at 18:05
52

A slightly different regex approach:

var afterSlashChars = id.match(/\/([^\/]+)\/?$/)[1];

Breaking down this regex:

\/ match a slash
(  start of a captured group within the match
[^\/] match a non-slash character
+ match one of more of the non-slash characters
)  end of the captured group
\/? allow one optional / at the end of the string
$  match to the end of the string

The [1] then retrieves the first captured group within the match

Working snippet:

var id = 'http://www.google.com/m8/feeds/contacts/myemail%40gmail.com/base/nabb80191e23b7d9';

var afterSlashChars = id.match(/\/([^\/]+)\/?$/)[1];

// display result
document.write(afterSlashChars);
jfriend00
  • 637,040
  • 88
  • 896
  • 906
  • Explanations make your answer much more valuable, than accepted one. Thanks! – Ilia Sep 24 '14 at 11:56
  • @IliaRostovtsev: Unfortunately, the explanation is wrong, and so is the answer. Non-greedy quantifiers don't guarantee the shortest possible match, because they only affect where the match *ends*, not where it begins. This regex matches from the *first* slash to the end. – Alan Moore Feb 22 '16 at 07:17
  • @Alan Moore I don't know why I picked this answer, probably because it fitted me at the moment. Thank you for your explanation. – Ilia Feb 22 '16 at 07:20
  • @AlanMoore - I modified the answer and added a working snippet. – jfriend00 Feb 22 '16 at 07:21
  • @IliaRostovtsev - I modified the answer and added a working snippet. – jfriend00 Feb 22 '16 at 07:23
  • Okay, now it *is* the best answer. :D – Alan Moore Feb 22 '16 at 07:41
  • @jfriend00 great answer and awesome explanation around your RegEx! – Paul Jul 22 '16 at 16:11
28

Just in case someone else comes across this thread and is looking for a simple JS solution:

id.split('/').pop(-1)

ramumb
  • 381
  • 3
  • 3
25

this is easy to understand (?!.*/).+

let me explain:

first, lets match everything that has a slash at the end, ok? that's the part we don't want

.*/ matches everything until the last slash

then, we make a "Negative lookahead" (?!) to say "I don't want this, discard it"

(?!.*) this is "Negative lookahead"

Now we can happily take whatever is next to what we don't want with this .+

YOU MAY NEED TO ESCAPE THE / SO IT BECOMES:

(?!.*\/).+

lalo
  • 831
  • 2
  • 10
  • 14
13

this regexp: [^\/]+$ - works like a champ:

var id = ".../base/nabb80191e23b7d9"

result = id.match(/[^\/]+$/)[0];

// results -> "nabb80191e23b7d9"
Kamil Kiełczewski
  • 71,169
  • 26
  • 324
  • 295
Andy
  • 1,106
  • 1
  • 11
  • 25
9

This should work:

last = id.match(/\/([^/]*)$/)[1];
//=> nabb80191e23b7d9
anubhava
  • 713,503
  • 59
  • 514
  • 593
  • 1
    Good answer. In Perl, regex delimeters must be escaped, even in character classes, otherwise it wouldn't find the regex to be parsed. –  Nov 04 '13 at 21:25
7

Don't know JS, using others examples (and a guess) -

id = id.match(/[^\/]*$/); // [0] optional ?

  • Just for the record, the match function returns an array.Using [0] is going to return just the first one. It's not optional. – brduca Sep 11 '14 at 17:07
  • There are no capture groups in this regex, isn't match overloaded to return the whole match if no index is referenced? –  Sep 30 '14 at 23:27
3

Why not use replace?

"http://google.com/aaa".replace(/(.*\/)*/,"")

yields "aaa"

Nimrod
  • 342
  • 2
  • 7