-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
  • 741,623
  • 53
  • 500
  • 612
FabricioG
  • 3,107
  • 6
  • 35
  • 74

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
  • 741,623
  • 53
  • 500
  • 612
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,384
  • 1
  • 9
  • 22