isUrl
验证是否为有效的URL
URL验证
✓有效URL
函数签名
typescript
interface IsUrlOptions {
requireProtocol?: boolean
protocols?: string[]
}
function isUrl(url: string, options?: IsUrlOptions): boolean参数
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
url | string | 是 | 要验证的URL字符串 |
options | IsUrlOptions | 否 | 验证选项 |
返回值
| 类型 | 说明 |
|---|---|
boolean | 是否为有效URL |
工作原理
- 检查输入是否为空或非字符串
- 根据requireProtocol选项决定是否添加临时协议
- 使用正则表达式验证URL基本格式
- 验证协议是否在允许列表中
- 验证主机名是否存在
- 返回验证结果