Open Pcjmy opened 1 year ago
const isValidUrl = (url) => { const urlRegex = /^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:((?:(?=[^.-])[\w-]+\.)*[\w-]{2,}\.?(?=[^.-])(?:\.[a-z]{2,})?))(:\d{1,5})?(\/\S*)?$/i; return urlRegex.test(url); } console.log(isValidUrl('https://www.example.com/path/to/resource')); // true console.log(isValidUrl('ftp://example.com/file.txt')); // true console.log(isValidUrl('http://localhost:8080')); // true console.log(isValidUrl('invalid url')); // false