3 判断是否为合法的URL
中等 通过率:42.50% 时间限制:3秒 空间限制:256M
描述
请补全JavaScript代码,要求以Boolean的形式返回字符串参数是否为合法的URL格式。
注意:1. 协议仅为HTTP(S)
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
</head>
<body>
<script type="text/javascript">
// 开始符 ^
// 协议部分http(s):// 表示为((https|http|ftp|rtsp|mms)?:\/\/)
// 域名部分 表示为(([A-Za-z0-9]+-[A-Za-z0-9]+|[A-Za-z0-9]+)\.)+
// 顶级域名com cn等为2-6位 表示为([a-zA-Z]{2,6})
// 端口部分 表示为(:\d+)?, ?表示0次或1次
// 请求路径如/login 表示为 (\/.*)?
// 问号传参及哈希值如?age=1 表示为 (\?.*)?和(#.*)?
// 结束符 $
const _isUrl = url => {
// 补全代码
let reg = /^((https|http|ftp|rtsp|mms)?:\/\/)(([A-Za-z0-9]+-[A-Za-z0-9]+|[A-Za-z0-9]+)\.)+([A-Za-z]{2,6})(:\d+)?(\/.*)?(\?.*)?(#.*)?$/
return reg.test(url)
}
console.log(_deleteRepeat([-1, 1, 2, 2]))
</script>
</body>
</html>