Skip to content

Instantly share code, notes, and snippets.

@darkmavis1980
Last active December 22, 2022 14:04
Show Gist options
  • Save darkmavis1980/50a4d20a6f9f58b95c10774d5f252ac4 to your computer and use it in GitHub Desktop.
Save darkmavis1980/50a4d20a6f9f58b95c10774d5f252ac4 to your computer and use it in GitHub Desktop.
Matches either hubspot domain or hs-sites (sandboxes)
// url => https://regex101.com/r/jhw0wy/1
const regex = /https:\/\/([a-z\.]{4})?([0-9]+.hs-sites|hubspot).com[\/]?/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('https:\/\/([a-z\.]{4})?([0-9]+.hs-sites|hubspot).com[\/]?', 'gm')
const str = `https://hubspot.com/
https://123456.hs-sites.com/
https://google.com`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment