Vimeo seems to sometimes have URLs of the form https://vimeo.com/12345 and sometimes URLs with an extra backslash followed by a code, like https://vimeo.com/12345/6d789.
In the latter case, with the extra slash and value, that happens to differ from their embed code where the URL needs to look like https://vimeo.com/12345?h=6d789.
In my JavaScript, where I'm taking these URLs and creating embeds I need to replace the final / with ?h= but only if that final element is there. If it's like the first URL, with no extra / and value, I want to leave the final slash alone.
What might be a simple JavaScript expression that takes a Vimeo URL and does the correct replacement, but only if needed?
Thanks.
Here you go
let vimeoUrls = {
url1 : "https://vimeo.com/12345",
url2 : "https://vimeo.com/12345/6d789"
}
for(url of Object.values(vimeoUrls)){
if(url.split("/").length<5)
console.log(url) //return in production
let urlSplit = url.split("/")
if(url.split("/").length>4) //delete this line in production
console.log(`${urlSplit[0]}//
${urlSplit[2]}/${urlSplit[3]}/?
h=${urlSplit[4]}`)