Youtube description characters are quiet expensive so we are trying to find a way to replace all youtube link entries in e textarea for the shorter ones.
Youtube's short url is "http://youtu.be/"+ [video id].
The file I'm loading contains all types of youtube links and the outcome of this script should be to shorten all youtube links found in the file.
Here is an example of the file:
Best Commercials Ever
10. T-Mobile Super Bowl 2016 Commercial:
https://www.youtube.com/watch?v=OUVFxBd8OCI&ab_channel=EpicExperiment
9. Butterfinger Super Bowl Commercial 2016:
https://www.youtube.com/watch?v=mXLWAAn7gEk
My script should look up this video and return:
Best Commercials Ever
10. T-Mobile Super Bowl 2016 Commercial: youtu.be/OUVFxBd8OCI
9. Butterfinger Super Bowl Commercial 2016: youtu.be/mXLWAAn7gEk
With this regex I am able to find pretty much any youtube link and transform it to the shorten one by extracting the video id:
var videoid = url.match(/(?:https?:\/{2})?(?:w{3}\.)?youtu(?:be)?\.(?:com|be)(?:\/watch\?v=|\/)([^\s&]+)/);
if(videoid != null) {
return "http://youtu.be/" + videoid[1];
} else {
console.log("The youtube url is not valid.");
}
However this function will only work with a simple string and not with a multiline one (txt file). Now I need something that would run this function inside the text file and replace all matches...
Thanks in advance for your help!