当前位置:网站首页>js正則判斷域名或者IP的端口路徑是否正確
js正則判斷域名或者IP的端口路徑是否正確
2022-04-23 15:47:00 【維多利亞少年-】
通過js正則來判斷ip端口路徑http://127.0.0.1:3000/list或者https://www.baidu.com:8080/list是否正確
const reg = new RegExp(/^http(s)?:\/\/((www\.)?[a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9]{0,62})|(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])\.((1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.){2}(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d))\:([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-5]{2}[0-3][0-5])\/+[a-zA-Z0-9]/)
reg.test(“https://www.aaaa.com:2000/105”) // true
reg.test(“https://127.0.0.1:2000/105”) // true
reg.test(“https://127.0.0.1:2000/System/list”) // true
reg.test(“https://127.0.0.1:2000/dict/list”) // true
reg.test(“http://127.0.0.1:2000/dict/list”) // true
通過校驗的完整url,可以使用下面的方法來提取其中的ip、端口。路徑
// 獲取ip地址、端口、路勁
getUrlComponent (url) {
let res = {
}
if (url.indexOf('//') !== -1 || url.indexOf(':') !== -1 || url.indexOf('.') !== -1) {
// 不截取http
let str = url.indexOf('//') !== -1 ? url.substr(url.indexOf('//') + 2) : url
res.ip = str.indexOf(':') !== -1 ? str.substr(0, str.indexOf(':')) : str.indexOf('/') !== -1 ? str.substr(0, str.indexOf('/')) : str
res.port = str.indexOf('/') !== -1 ? str.slice(str.indexOf(':') + 1, str.indexOf('/')) : str.substr(str.indexOf(':') + 1)
res.path = res.port !== '' && str.indexOf(res.port) !== -1 ? str.substr(str.indexOf(res.port) + res.port.length) : str.indexOf('/') !== -1 ? str.substr(str.indexOf('/')) : ''
}
return res
}
版权声明
本文为[維多利亞少年-]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231547082639.html
边栏推荐
- Fastjon2 here he is, the performance is significantly improved, and he can fight for another ten years
- Large factory technology implementation | industry solution series tutorials
- Go语言数组,指针,结构体
- C#,贝尔数(Bell Number)的计算方法与源程序
- 计算某字符出现次数
- mysql乐观锁解决并发冲突
- 编译,连接 -- 笔记
- For examination
- Spark 算子之交集、并集、差集
- pywintypes. com_ Error: (- 2147221020, 'invalid syntax', none, none)
猜你喜欢
随机推荐
Go语言条件,循环,函数
Date date calculation in shell script
Single architecture system re architecture
IronPDF for . NET 2022.4.5455
Mobile finance (for personal use)
新动态:SmartMesh和MeshBox的合作新动向
MySQL Cluster Mode and application scenario
leetcode-396 旋转函数
CVPR 2022 quality paper sharing
Redis master-slave replication process
Treatment of idempotency
布隆过滤器在亿级流量电商系统的应用
Go language, array, pointer, structure
时序模型:门控循环单元网络(GRU)
Extract non duplicate integers
为啥禁用外键约束
Upgrade MySQL 5.1 to 5.66
Go concurrency and channel
Spark 算子之distinct使用
MySQL集群模式與應用場景