0

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.

2
  • Is it always in this format? split('/') would give you what you need if it doesn't change. Commented Nov 30, 2015 at 17:15
  • After reading about split() you'll know ... Commented Nov 30, 2015 at 17:15

1 Answer 1

1

If your URL format is consitent just use the following

var url  =  "https://somesite.com/video/123456";

var id = url.split("/")[4]

Sign up to request clarification or add additional context in comments.

1 Comment

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.