Skip to content

Instantly share code, notes, and snippets.

@adamculpepper
Created October 28, 2015 19:54
Show Gist options
  • Save adamculpepper/e415c6e9dd8afb5454e6 to your computer and use it in GitHub Desktop.
Save adamculpepper/e415c6e9dd8afb5454e6 to your computer and use it in GitHub Desktop.
JavaScript: Strip off slugs from a URL
function cutUrl(str, num) {
var matched = str.match("([^/]*\/){" + num + "}");
return matched ? matched[0] : str; /* or null */
}
//usage
//var url = location.pathname;
var url = "http://domain.com.com/page/subpage/fsdf/dsfg/dgff/gsd/fsdfsg/sddfsdf/gds/fsdf/";
console.log( cutUrl(url, 4) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment