const isValidUrl = url => {
try {
return Boolean(new URL(url));
}
catch(e){
return false;
}
}
It doesn't check if there's a top-level domain
> new URL('https://google');
URL {origin: 'https://google', protocol: 'https:', username: '', password: '', host: 'google', …}
so you should still use a regex
https://stackoverflow.com/a/3809435/8479344
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)