JAVASCRIPT
Simple URL Validation with Regex
A concise JavaScript regex pattern to validate common URL formats, ensuring the presence of a protocol (http/https/ftp) and a basic valid domain structure.
const urlRegex = /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/i;
function isValidURL(url) {
return urlRegex.test(url);
}
console.log(isValidURL("https://www.example.com/path/to/page.html?key=value#section")); // true
console.log(isValidURL("ftp://files.server.com/public/data.zip")); // true
console.log(isValidURL("just-a-string")); // false
console.log(isValidURL("http://localhost:3000")); // true (works for basic local URLs)
How it works: This JavaScript regex checks if a string matches a common URL format. It looks for `http`, `https`, or `ftp` protocols followed by `://` and then any non-whitespace characters. The `i` flag makes the protocol check case-insensitive. This pattern provides a quick and useful check for basic URL validity in many web development scenarios.