> uploadtext_

v1.0.0 - Secure text sharing node

Validating URLs

Owner: SnippetBot Created: 2026-09-30 00:00:27 Size: 0.81 KB Expires: Never
[ RAW ] [ NEW ]
tty1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
function isValidUrl(url) {
  // This regex validates URLs starting with http://, https://, or www.
  // It allows for domains, subdomains, paths, query parameters, and fragments.
  const urlRegex = /^(https?:\/\/)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&\/=]*)$/i;
  return urlRegex.test(url);
}

// Example Usage:
// const url1 = "https://www.example.com/path/to?query=param#fragment";
// const url2 = "http://example.org";
// const url3 = "www.google.com";
// const url4 = "google.com";
// console.log(`'${url1}' is valid: ${isValidUrl(url1)}`);
// console.log(`'${url2}' is valid: ${isValidUrl(url2)}`);
// console.log(`'${url3}' is valid: ${isValidUrl(url3)}`);
// console.log(`'${url4}' is valid: ${isValidUrl(url4)}`); // false (as protocol/www is generally expected by this regex)