Skip to content

Instantly share code, notes, and snippets.

@codingforentrepreneurs
Created July 12, 2023 19:58
Show Gist options
  • Save codingforentrepreneurs/13e153d28e8c15b7b97331f7813fae9b to your computer and use it in GitHub Desktop.
Save codingforentrepreneurs/13e153d28e8c15b7b97331f7813fae9b to your computer and use it in GitHub Desktop.
Verify if a URL is valid or not via Regex in Next.js Server Components

Verify if a URL is valid or not via Regex in Next.js

Usage:

const isValid = await isValidURL("http://mytest-domain.com", ["mytest-domain.com"])
// false

const isValid = await isValidURL("http://mytest.com", ["mytest-domain.com"])
// true

const isValid = await isValidURL("http://localhost:3000")
// false
export default async function isValidURL(url, disallowedDomains) {
// Construct a regular expression pattern to match disallowed domains
const disallowedPattern = `^https?:\\/\\/(?:${disallowedDomains.join('|')})\\b`;
let disallowedRegex = new RegExp(disallowedPattern, 'i');
// Regular expression pattern to match a URL (excluding localhost)
const urlPattern = /^(https?:\/\/)?((?!localhost)[\w.-]+)\.([a-z]{2,})(:\d{1,5})?(\/.*)?$/i;
let urlRegex = new RegExp(urlPattern);
// Test the URL against both URL pattern and disallowed domain pattern
return urlRegex.test(url) && !disallowedRegex.test(url);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment