views:

44

answers:

3
"/organizations/1/media/videos/11"

I would like to just grab that 1 . Not sure how to exactly do that.

Any ideas?

+4  A: 

just split the location.pathname and grab that index. this is vanilla javascript

var num = window.location.pathname.split("/")[2]
contagious
Yes you got it ! Thanks so much. It's actually [2] though. Organizations would be 1.
Trip
edited to the correct index. forgot the leading "/"
contagious
Hope the URLS never change.
epascarello
A: 
var myString = "/organizations/1/media/videos/11";
var myArr = myString.split('/');
alert(myArr[2]);

That should be sufficient, but it definitely depends on what the pattern of your URLs will be.

Demo: http://jsfiddle.net/XYEAh/

Ender
A: 

Regular Expression Solution:

var str = "/organizations/1/media/videos/11";

var re = /\/(\d+)\//;
var num = null;

var data = str.match(re);
if(data){
  num = parseInt( data[1], 10 );
}

alert(num);
epascarello