-1

I have a url and just want to get the id:

https://web.microsoftstream.com/video/223ac74c-0a2f-4f36-b78b-a8ad8a6e3009

Guess I could just look for the '/' at the end and split up the string into substring. What would be a more elegant/better way to just get the id: 223ac74c-0a2f-4f36-b78b-a8ad8a6e3009?

2
  • Match /\d+$/? split is fine too Commented May 18, 2018 at 0:50
  • location.pathname.split('/').pop() is one approach (assuming no hash or query params in url) Commented May 18, 2018 at 0:55

1 Answer 1

5

You can use Array.prototype.substring or regex

let url = 'https://web.microsoftstream.com/video/112233444';
let id = url.substring(url.lastIndexOf('/') + 1);
console.log(id);

// Or using regex
id = url.match(/\d+$/)[0];
console.log(id);

// If your id is some hash or uuid then
url  = 'https://web.microsoftstream.com/video/223ac74c-0a2f-4f36-b78b-a8ad8a6e3009';
console.log(url.match(/video\/(.*)$/)[1]);

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

5 Comments

awesome dude thanks
I updated answer, don't forget to add $ at the end of regex otherwise it will match any number inside url
What if the id looks like this:223ac74c-0a2f-4f36-b78b-a8ad8a6e3009
@bierhier Updated answer. Added similar scenario
many thanks, good on ya

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.