Skip to content

Instantly share code, notes, and snippets.

@pernydev
Last active February 2, 2023 07:45
Show Gist options
  • Select an option

  • Save pernydev/1d5ed26ab8dcdd8d4840aee114b1cce7 to your computer and use it in GitHub Desktop.

Select an option

Save pernydev/1d5ed26ab8dcdd8d4840aee114b1cce7 to your computer and use it in GitHub Desktop.
Link filter for OpenAI Discord (In JavaScript)
function getDomainsFromString(stringWithUrls) {
const whitelist = ["openai.com", "stackoverflow.com"];
const urlRegex = /https?:\/\/(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+/g;
const urls = stringWithUrls.match(urlRegex);
const domains = urls.map(url => {
const domainRegex = /(?<domain>[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,})/;
const domain = url.match(domainRegex).groups.domain;
return domain;
});
const filteredDomains = domains.filter(domain => whitelist.includes(domain));
return filteredDomains;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment