-1

I currently have a url path like so:

var str = "http://stackoverflow.com/i-like-upvotes/you-do-too";

I wish to delete/remove all the string up to the last / slash.

If I wanted to replace the last slash I'd get it like this:

console.log(str.replace(/\/(?=[^\/]*$)/, '#'));

But I would like to delete everything up to the last slash and I can't figure it out. In the above example I'd get back

you-do-too

Any ideas?

Barmar
  • 669,327
  • 51
  • 454
  • 560
FabricioG
  • 2,585
  • 4
  • 28
  • 58

2 Answers2

2

var str = "http://stackoverflow.com/i-like-upvotes/you-do-too";
console.log(str.replace(/^.*\//, ''));
  • ^ matches the beginning of the string.
  • .* matches anything.
  • \/ matches slash.

Since .* is greedy, this will match everything up to the last slash. We delete it by replacing with an empty string.

Barmar
  • 669,327
  • 51
  • 454
  • 560
0

I know it's not beautiful, but you can do str.split('/').pop() to get the last part of the URL.
No un-humanly readable regex

Obzi
  • 2,326
  • 1
  • 8
  • 21