// Function to validate a URL using a regular expression
function isValidURL(url) {
const urlRegex = /^(ftp|http|https):\/\/[a-zA-Z0-9-]+(\.[a-zA-Z]{2,})+(\/[^\s]*)?$/;
return urlRegex.test(url);
}
// Example URL to validate
const websiteURL = 'https://www.example.com';
// Check if the URL is valid using the isValidURL function
console.log('Is Valid URL:', isValidURL(websiteURL));
code
JavaScript Validate URL
This JavaScript code defines a function isValidURL that uses a regular expression (urlRegex) to check if a given URL is valid. The provided example URL (https://www.example.com) is then checked for validity using this function. The comments provide explanations for each part of the code.
Regular Expression
URL Validation