I have a string like this "/blog/post1/"
how can i get the "post1" from this string in Jquery ?
I tried this but it returned empty.
var post_slug = $(this)[0].pathname.substring($(this)[0].pathname.lastIndexOf("/")).replace(/^\//, "");
You can do it like this.
var str = "/blog/post1/";
arr = str.split('/');
var res = arr[arr.length-2];
alert(res);
Use the JavaScript split function. Much simpler solution.
var url = "/blog/post1/";
var splitUrl = url.split("/");
// assuming known URL structure so always same index
var post_slug = splitUrl[splitUrl.length -2];
Working fiddle: http://jsfiddle.net/Rsrmk/
splitit!