我有一个网址,只想获得ID:
https://web.microsoftstream.com/video/223ac74c-0a2f-4f36-b78b-a8ad8a6e3009
猜猜我可以在最后查找'/'并将字符串拆分为子字符串。什么是更优雅/更好的方式来获得id:223ac74c-0a2f-4f36-b78b-a8ad8a6e3009?
答案 0 :(得分:4)
您可以使用Array.prototype.substring
或正则表达式
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]);