RegEx - Get All Characters After Last Slash in URL

JavascriptRegex

Javascript Problem Overview


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'

Javascript Solutions


Solution 1 - Javascript

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);

Solution 2 - Javascript

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);

Solution 3 - Javascript

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

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

Solution 4 - Javascript

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:

(?!.*\/).+

Solution 5 - Javascript

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

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

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

// results -> "nabb80191e23b7d9"

Solution 6 - Javascript

This should work:

last = id.match(/\/([^/]*)$/)[1];
//=> nabb80191e23b7d9

Solution 7 - Javascript

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

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

Solution 8 - Javascript

Why not use replace?

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

yields "aaa"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionac360View Question on Stackoverflow
Solution 1 - JavascriptlonesomedayView Answer on Stackoverflow
Solution 2 - Javascriptjfriend00View Answer on Stackoverflow
Solution 3 - JavascriptramumbView Answer on Stackoverflow
Solution 4 - JavascriptlaloView Answer on Stackoverflow
Solution 5 - JavascriptAndyView Answer on Stackoverflow
Solution 6 - JavascriptanubhavaView Answer on Stackoverflow
Solution 7 - Javascriptuser557597View Answer on Stackoverflow
Solution 8 - JavascriptNimrodView Answer on Stackoverflow