当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
考试考试自用
Timing model: gated cyclic unit network (Gru)
KNN, kmeans and GMM
Config learning notes component
CVPR 2022 优质论文分享
Introduction to dynamic programming of leetcode learning plan day3 (198213740)
PHP classes and objects
WPS品牌再升级专注国内,另两款国产软件低调出国门,却遭禁令
MySQL Cluster Mode and application scenario
布隆过滤器在亿级流量电商系统的应用
Go语言数组,指针,结构体
fatal error: torch/extension. h: No such file or directory
【第5节 if和for】
Metalife established a strategic partnership with ESTV and appointed its CEO Eric Yoon as a consultant
One brush 313 sword finger offer 06 Print linked list from end to end (E)
Spark 算子之coalesce与repartition
[backtrader source code analysis 18] Yahoo Py code comments and analysis (boring, interested in the code, you can refer to)
Deletes the least frequently occurring character in the string
Go并发和通道
时序模型:长短期记忆网络(LSTM)