The one-liner for the file you're already in
If your script is bound to a spreadsheet (meaning it was created from Extensions > Apps Script inside that file), SpreadsheetApp.getActiveSpreadsheet().getId() returns the ID without any URL parsing. The ID is a 44-character alphanumeric string that uniquely identifies the file in Drive, regardless of what you rename it later.
The first time I needed this, I spent ten minutes digging through Drive API docs before realizing the bound-script shortcut existed. If you're in a standalone script rather than a bound one, getActiveSpreadsheet() returns null, and you'll need openById() or openByUrl() with an explicit ID instead.
Once you have the ID, you can store it in PropertiesService.getScriptProperties() so a standalone utility script can reference the same file across runs without hardcoding the URL.