UNPKG

@mikezimm/fps-core-v7

Version:

Library of reusable core interfaces, types and constants migrated from fps-library-v2

75 lines (70 loc) 2.73 kB
/** * This could be modified to leverage an array of regex * @param url * @param regex1 * @param regex2 * @returns */ export function getDomain2Reg(url, regex1, regex2) { const match = url.match(regex1); if (match) return match[1]; const match2 = url.match(regex2); // 2024-09-22: Changed return from null to '' return match2 ? match2[1] : ''; } /** * To extract the site collection name from a SharePoint Online site URL, you can use the following JavaScript regular expression. This regex will capture the site collection name from the given URL structure. Here is the JavaScript code: javascript Copy code function getSiteCollectionName(url) { const regex = /https:\/\/[^.]+\.sharepoint\.com\/sites\/([^\/]+)/i; const match = url.match(regex); return match ? match[1] : null; } const url = "https://fuzzypawstech.sharepoint.com/sites/XYZ/MoreStuff"; const siteCollectionName = getSiteCollectionName(url); console.log(siteCollectionName); // Outputs: XYZ Explanation of the regex: https:\/\/ matches the https:// part of the URL. [^.]+ matches one or more characters that are not a dot, capturing the subdomain part (e.g., tenant). \.sharepoint\.com\/sites\/ matches .sharepoint.com/sites/. ([^\/]+) captures one or more characters that are not a forward slash (/), representing the site collection name. i is the case-insensitive flag. This regex will correctly extract the site collection name (e.g., XYZ) from the given SharePoint Online site URL. * @param url */ /** * NOTE: There is a better function for getting full absolute value of current site: getSiteCollectionUrlFromLink * You can also get it directly using the constant: CurrentSiteAbsolute * * url = 'https://tenant.sharepoint.com/sites/XYZ/MoreStuff' * output: XYZ * * @param url * @returns */ export function getCollectionUrl(url) { const regex = /https:\/\/[^.]+\.sharepoint\.com\/sites\/([^/]+)/i; const regex2 = /\/sites\/([^/]+)/; if (url) return getDomain2Reg(url, regex, regex2); // 2024-09-22: Changed return from null to '' return url ? getDomain2Reg(url, regex, regex2) : ''; } /** * * @param url * @returns */ export function getTeamsUrl(url) { const regex = /https:\/\/[^.]+\.sharepoint\.com\/teams\/([^/]+)/i; const regex2 = /\/teams\/([^/]+)/; if (url) return getDomain2Reg(url, regex, regex2); // 2024-09-22: Changed return from null to '' return url ? getDomain2Reg(url, regex, regex2) : ''; } //# sourceMappingURL=getSPOUrl.js.map