Hi !
you can juste use this function to extract it
function extractVideoId(url) {
//we take everything avec "v=" and stops if there is a caracter
// not between a-z (Maj and Min)
// not a number
let pattern = /v=([a-zA-Z0-9_-]+)/;
// Let's see if there is a match
let match = url.match(pattern);
// Check if there is a match and return the video ID if found
if (match && match[1]) {
return match[1];
} else {
// Not Found
return null
}
}
all you have to do is update your variable with a function. put our function and add there lines
let youtubeURL = "https://www.youtube.com/watch?v=4vLxWqE94l3accedea&t=1s";
return extractVideoId(youtubeURL);
it will return null if there is nothing.
It will Always take the value after v= and stops at & if there is ( & is not included in between a and z and not included in 0-9 defined in our regular expression, so it will basically stop there.
Thank you Yassine.tebib! I tested your snippet in vsCode and it seems to work correctly, but in Appfarm I get an error. Here is the code:
console.log("myDebug function");
function extractVideoId(url) {
//we take everything avec "v=" and stops if there is a caracter
// not between a-z (Maj and Min)
// not a number
let pattern = /v=([a-zA-Z0-9_-]+)/;
// Let's see if there is a match
let match = url.match(pattern);
console.log("myDebug found this: " + match)
// Check if there is a match and return the video ID if found
if (match && match[1]) {
return match[1];
} else {
// Not Found
return null
}
}
let returnURL = "https://www.youtube.com/embed/" + extractVideoId(youtubeURL);
console.log("myDebug returnURL: " + returnURL)
return returnURL;
This might be an embedded issue. You can whitelist youtube.com within the specific environment to allow embedding content from YouTube.
Go to your Environment settings.
Under Content Security, find the section labeled “Frame Targets.”
Add youtube URL to the list of whitelisted domains.
This should allow you to embed content from YouTube without any issues. Im not sure if you are using the iframe component or not, but check out the documentation for more information on using the iframe component.