当前位置:网站首页>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://blog.csdn.net/weixin_45532305/article/details/124302762
边栏推荐
- Recommended search common evaluation indicators
- [split of recursive number] n points K, split of limited range
- Partitionby of spark operator
- MySQL Cluster Mode and application scenario
- Merging of Shanzhai version [i]
- Load Balancer
- Large factory technology implementation | industry solution series tutorials
- 编译,连接 -- 笔记
- vim指定行注释和解注释
- Multitimer V2 reconstruction version | an infinitely scalable software timer
猜你喜欢
Recommended search common evaluation indicators
一刷314-剑指 Offer 09. 用两个栈实现队列(e)
贫困的无网地区怎么有钱建设网络?
实现缺省页面
Merging of Shanzhai version [i]
考试考试自用
Today's sleep quality record 76 points
CVPR 2022 quality paper sharing
导入地址表分析(根据库文件名求出:导入函数数量、函数序号、函数名称)
Multitimer V2 reconstruction version | an infinitely scalable software timer
随机推荐
北京某信护网蓝队面试题目
MySQL集群模式與應用場景
How do you think the fund is REITs? Is it safe to buy the fund through the bank
【自娱自乐】构造笔记 week 2
[AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
Config组件学习笔记
网站压测工具Apache-ab,webbench,Apache-Jemeter
字符串排序
utils.DeprecatedIn35 因升级可能取消,该如何办
[backtrader source code analysis 18] Yahoo Py code comments and analysis (boring, interested in the code, you can refer to)
单体架构系统重新架构
Spark 算子之partitionBy
网站建设与管理的基本概念
Go language, array, pointer, structure
Calculate the number of occurrences of a character
建设星际计算网络的愿景
Basic concepts of website construction and management
负载均衡器
Go语言条件,循环,函数
Open source project recommendation: 3D point cloud processing software paraview, based on QT and VTK