DriveApp is really missing getFileByUrl (as well as the folder for that matter). You might want to open a request for improvement in the application script tracker .
But what I do in my scripts (since these openByUrl functions openByUrl somewhat new) is to get the identifier using regular expressions. Like this.
function getIdFromUrl(url) { return url.match(/[-\w]{25,}/); }
This regular expression works for any Google URL I tried: drive URL for folders and files, Fusion spreadsheets, spreadsheets, documents, presentations, etc. It just searches for everything in a string that is βsimilarβ to a Google key. That is, any sufficiently large string containing only (Google key) valid characters.
Furthermore, it works even if it gets the identifier directly, not the URL. This is useful when you request a link from a user, as some may insert an identifier instead of a URL, and it still works.
--edit
There are some other answers and comments that relate to some extreme cases that I have never encountered, but may happen, for example, an attempt to get the folder ID by the URL of the subfolder, or when you have a G-Suite domain 25+ characters long In these cases, you can use a more strict regular expression.
Having briefly reviewed the suggestions below, I recommend the following /[-\w]{25,}$/ , since it is still very simple and should take these cases into account.
source share