Home > Back-end >  How can I extract only google drive folder id in url with javascript regular expression?
How can I extract only google drive folder id in url with javascript regular expression?

Time:10-20

https://drive.google.com/drive/folders/19YDveZO2mo_MsNPW9qElpGwsdjbl5tppDhzL?ths=true https://drive.google.com/drive/folders/19YDveZO2mo_MsNPW9qElpGwsdjbl5tppDhzL

I want to extract only 19YDveZO2mo_MsNPW9qElpGwsdjbl5tppDhzL from the above two URLs. I'm curious about a JavaScript regular expression that can be applied in common to both URLs.

CodePudding user response:

One way to do this using regex is with the positive lookbehind operator

// Positive lookbehind "folders/" and stop at "?"

const regex = /(?<=folders\/)[^? \n\r\t]*/;

(?<=) is the positive lookbehind operator: It matches a group before the main expression without including it in the result. The rest of the regex is matching any amount of characters until a ?, <space>, \n, \r, \t.

Unfortunately the positive lookbehind operator is not always available in JS. I think the reason behind this is that processing strings in JS can be done using the language itself. If I was to do it, I'd go for the JS option:

const url1 = "https://drive.google.com/drive/folders/19YDveZO2mo_MsNPW9qElpGwsdjbl5tppDhzL?ths=true";
const url2 = "https://drive.google.com/drive/folders/19YDveZO2mo_MsNPW9qElpGwsdjbl5tppDhzL";

// Using regex

const regex = /(?<=folders\/)[^? \n\r\t]*/;
console.log("Regex: ", url1.match(regex), url2.match(regex));

// Using JS   

const extractHash = (string) => {
  return string.split("folders/")[1].split("?")[0];
};
console.log("JS: ", extractHash(url1), extractHash(url2));

  • Related