Skip to content

Instantly share code, notes, and snippets.

@CoolS2
Last active August 1, 2016 12:56
Show Gist options
  • Select an option

  • Save CoolS2/e6d7fd201e45eeacc715812696e4aa24 to your computer and use it in GitHub Desktop.

Select an option

Save CoolS2/e6d7fd201e45eeacc715812696e4aa24 to your computer and use it in GitHub Desktop.
Text from url on javascript
// Option 1
var url = "http://mywebsite.com/extractMe/test";
var extractedText = url.split("/")[3];
​// Option 2
// If when a trailing slash is present you want to return "test", use this code
var url = "http://mywebsite.com/extractMe/test/";
var urlAry = url.split("/");
var extractedText = urlAry[urlAry.length - 2];
​// Option 3
// If when a trailing slash is present you want to return "extractMe", use this:
var url = "http://mywebsite.com/extractMe/test/";
var urlAry = url.split("/");
var positionModifier = (url.charAt(url.length-1) == "/") ? 3 : 2;
var extractedText = urlAry[urlAry.length - positionModifier];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment