i would like to extract part of a url and not sure what is the best practice for going about that. the url input format would be like: https://somesite.com/video/123456 i would like to extract the video id which is 123456so i can use at another place. any thoughts or suggestion would be appreciated. I am not sure how to use a split or if using regex would be better.
1 Answer
If your URL format is consitent just use the following
var url = "https://somesite.com/video/123456";
var id = url.split("/")[4]
1 Comment
Logan Bentley
I'd recommend adding a general solution as well, not just the specific one. Something like this
var id = window.location.href.split('?')[0].split('/').pop();
split('/')would give you what you need if it doesn't change.split()you'll know ...