Created
October 28, 2015 19:54
-
-
Save adamculpepper/e415c6e9dd8afb5454e6 to your computer and use it in GitHub Desktop.
JavaScript: Strip off slugs from a URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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