Get Youtube V=ID Script

Hey all. I’ve got an Automation script seemingly close to get the Youtube Video ID from a full URL which I found here.

Script from link above
<script type="text/javascript">
function youtube_parser(url){
    var regExp = /^.*((\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/;
    var match = url.match(regExp);
    return (match&&match[7].length==11)? match[7] : false;


I’m trying to take a Youtube URL in an existing field: myInput

and strip out only YAIlMUX9VLg to pass onto the next step (or output to the field Video ID, doesn’t matter).

I’m feel like I’m so close but at the end of my copy/paste/adapt coding skills. Here’s my code:

let myTable = base.getTable("Video Tracker");
let query = await myTable.selectRecordsAsync();
let myUpdateField = myTable.getField('Video ID');

function youtube_parser(myInput){
    var regExp = /^.*(youtu\.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
    var match = myInput.match(regExp);
if (match && match[2].length == 11) {
  return match[2];
} else {
var ytOUT = youtube_parser;
output.set('myOutput', ytOUT);

And the error I get that I cannot make heads or tails of:


TypeError: Invalid arguments passed to output.set(key, value): • value should be a JSON-serializable type, not a function (undefined)

at main on line 16

Here’s my Edit Script screen overall

and the Automation overall if it helps:

Thanks for any help.

I think this is where the issue is - I think it needs to be:

var ytOUT = youtube_parser(myUpdateField);

1 Like

Maybe I’m missing something, but I get the sense that you’re missing something that definitively sets the value of the URL that needs to be parsed. I see you open the table and get the records, but where do you decide which record to use in the parse process? I assume you want to run this process for all records, perhaps?

Another thing - to get the value of the youtube ID from the URL, you don’t need the youtube_parser() function; rather, I think this is all you need given the URL is in a variable…

  let myInput = "";
  let ytOUT = myInput.split("?v=")[1];
  output.set('myOutput', ytOUT);
1 Like

Thanks, Bill. My intent of using the youtube_parser script was because it could handle so many variations of the initial URL. Just trying to prevent future human error. I’m wanting the Automation to just always be looking for that Live URL to land to be able to strip the video ID out into the field for that string. I can use this for a bunch of other stuff (Zapier, buttons, edit links). That’s not necessary, obviously.

Types of URLS

These are the types of URLs supported;hl=en_US&amp;rel=0


I wondered if I hadn’t selected the right record, thanks. Also, I was missing that youtube_parser(liveURL) bit.

After a bit more guessing and testing, I found this article that helped me revise the input variables I had entered on the left (wasn’t calling those and didn’t know how till I saw the example).

Thanks for pointing me in the right direction. After turning this automation on I’ve realized that it’s not going to process all my existing entries but only new ones :thinking:. Not the end of the world I suppose.

Here’s my working script (for new entries)

let inputConfig = input.config();
var liveURL = inputConfig.myInput;

function youtube_parser(liveURL){
    var regExp = /^.*(youtu\.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
    var match = liveURL.match(regExp);
if (match && match[2].length == 11) {
  return match[2];
} else {
var ytOUT = youtube_parser(liveURL);
output.set('myOutput', ytOUT);

Yep, the variants do require regex.

Just create a Script Block version of the same process and allow it to process all existing records.

1 Like

Great idea. You’re awesome.