function isValidHttpUrl(string) {
try {
const newUrl = new URL(string);
return newUrl.protocol === 'http:' || newUrl.protocol === 'https:';
} catch (err) {
return false;
}
}
console.log(isValidHttpUrl('https://www.baidu.com')); // true
console.log(isValidHttpUrl('mailto://[email protected]')); // false
console.log(isValidHttpUrl('baidu')); // false
标签:baidu,console,log,isValidHttpUrl,URL,验证,JS,false
From: https://www.cnblogs.com/nuomibaibai/p/17656916.html