0

Is possible to get the last value in URL results in the following string:

http://www.example.com/snippets/rBN6JNO/

My RegEx is matching the whole string

\/.+\/

It matches:

//www.example.com/snippets/rBN6JNO/

I want to get the last value:

rBN6JNO

Or should I use another method?

James Mwase
  • 828
  • 1
  • 16
  • 29
1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231

4 Answers4

4

You can split your URL with /, and then access the last object of the splited URL.

Try the following:

var url = "http://www.example.com/snippets/rBN6JNO";
var splitedUrl = url.split('/');   
var lastPath = splitedUrl[splitedUrl.length - 1]; //rBN6JNO
Mistalis
  • 17,793
  • 13
  • 73
  • 97
2

Try this using javaScript you can do it,

var URL_STRING = 'http://www.example.com/snippets/rBN6JNO/'
URL_STRING.split('/').slice(3)
Saurabh Agrawal
  • 7,581
  • 2
  • 27
  • 51
2
/([a-z\d]+)(\/*|)$/i

And see "$1"

Choo Hwan
  • 416
  • 3
  • 12
2

To avoid to create an array, you can do this with string methods only - using substr() and lastIndexOf():

s = s.replace(/\/$/,"");
console.log(s.substr(s.lastIndexOf("/") + 1));
baao
  • 71,625
  • 17
  • 143
  • 203