1function validURL(str) {
2 var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
3 '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
4 '((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
5 '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
6 '(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
7 '(\\#[-a-z\\d_]*)?$','i'); // fragment locator
8 return !!pattern.test(str);
9}
1function isValidHttpUrl(string) {
2 let url;
3
4 try {
5 url = new URL(string);
6 } catch (_) {
7 return false;
8 }
9
10 return url.protocol === "http:" || url.protocol === "https:";
11}