Skip to content

Instantly share code, notes, and snippets.

@mark05e
Created May 29, 2023 19:54
Show Gist options
  • Save mark05e/ef51abecb0a2f7461ce666faffc1eaff to your computer and use it in GitHub Desktop.
Save mark05e/ef51abecb0a2f7461ce666faffc1eaff to your computer and use it in GitHub Desktop.
/**
* Extracts the ID from a Google Docs URL.
*
* @param {string} url - The Google Docs URL.
* @returns {string|null} - The extracted ID from the URL, or null if no ID is found.
*/
function getIdFromUrl(url) {
// Use a regular expression to match and extract the ID from the URL
let result = url.match(/[-\w]{25,}(?!.*[-\w]{25,})/);
if (result) {
// If a match is found, return the first matched value, which represents the ID
return result[0];
} else {
// If no match is found, return null to indicate the absence of an ID
return null;
}
}
function getIdFromUrl_test() {
let url =
console.log(getIdFromUrl(url)) // 1sbw70fxipObXXXXXX_zKwSzXXXXXSu0p
url = 'https://drive.google.com/'
console.log(getIdFromUrl(url)) // null
url = '1iexHLQhYstU1XXXXXXXXjIGhJ1JC5VT1'
console.log(getIdFromUrl(url)) // 1iexHLQhYstU1XXXXXXXXjIGhJ1JC5VT1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment