Public
Edited
Oct 26, 2022
2 stars
Insert cell
Insert cell
Insert cell
function getURLFromURL(str) {
const urlRegex =
/(https?:\/\/([\w_-]+(?:(?:\.[\w_-]+)+))(?:[\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-]))/gim;

let segments = [];
try {
const url = new URL(str);

const pathSegments = url.pathname
.split("/")
// Path needs decoding
.map(decodeURIComponent);

// Search params are already decoded
let params = Array.from(url.searchParams.values());

segments = [...pathSegments, ...params];
} catch (error) {
return;
}

let urls = segments.flatMap((s) => s.match(urlRegex)).filter(Boolean);

return urls[0];
}
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more