-1

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?

1.21 gigawatts
  • 14,347
  • 30
  • 103
  • 209

4 Answers4

2

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,145
  • 13
  • 72
  • 95
1

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,230
  • 2
  • 22
  • 46
1
/([a-z\d]+)(\/*|)$/i

And see "$1"

Choo Hwan
  • 406
  • 3
  • 12
1

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
  • 67,185
  • 15
  • 124
  • 181