I want Javascript regex for extracting spreadsheet ID and sheet ID from google sheets URL. The URL for sheets.google spreadsheet looks like: .
This Link describes what regex to use for extracting spreadsheet ID and sheet ID. I tried them in Javascript but it is not working. Here is code that I tried:
var spreadsheetId = new RegExp("/spreadsheets/d/([a-zA-Z0-9-_]+)").exec(resourceUrl);
var sheetId = new RegExp("[#&]gid=([0-9]+)").exec(resourceUrl);
I want Javascript regex for extracting spreadsheet ID and sheet ID from google sheets URL. The URL for sheets.google. spreadsheet looks like: https://docs.google./spreadsheets/d/1QOKbrUz7AWUwT-hNfWd1Pxf__QObYCZ1rD2CKMjpnGw/edit#gid=0.
This Link describes what regex to use for extracting spreadsheet ID and sheet ID. I tried them in Javascript but it is not working. Here is code that I tried:
var spreadsheetId = new RegExp("/spreadsheets/d/([a-zA-Z0-9-_]+)").exec(resourceUrl);
var sheetId = new RegExp("[#&]gid=([0-9]+)").exec(resourceUrl);
Share
Improve this question
asked Jan 18, 2017 at 10:44
user41451user41451
2692 gold badges4 silver badges12 bronze badges
3 Answers
Reset to default 8Assuming that the Google Spreadsheet ID has more than 15 characters, the regex would be like this. The GID may or may not be available in a URL.
var resourceUrl = "https://docs.google./spreadsheets/d/1QOKbrUz7AWUwT-hNfWd1Pxf__QObYCZ1rD2CKMjpnGw/edit#gid=0";
var matches = /\/([\w-_]{15,})\/(.*?gid=(\d+))?/.exec(resourceUrl);
if (matches) {
console.log("Spreadsheet: " + matches[1]);
console.log("Sheet: " + matches[3]);
}
If it succeeds, the exec()
method places the matching characters in its indexes. Index 0 is the full string of matched characters. Other indexes (1, 2, ...) are the characters matched by parts of the regular expression delimited by parentheses, in the order they occur.
Your regular expressions match a larger part of the URL string, but the IDs are the parts delimited by parentheses. In both cases you have just one pair of parentheses hence they are in index 1. So the values you want are in
spreadsheetId = new RegExp("/spreadsheets/d/([a-zA-Z0-9-_]+)").exec(resourceUrl)[1];
sheetId = new RegExp("[#&]gid=([0-9]+)").exec(resourceUrl)[1];
(note the [1]
at the end of both instructions)
Something wrong with just looking for the segment following "/d/" ?
/.+\/d\/([^/]+)/
The capture group is the id