Last active
August 1, 2016 12:56
-
-
Save CoolS2/e6d7fd201e45eeacc715812696e4aa24 to your computer and use it in GitHub Desktop.
Text from url on javascript
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
| // 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