当前位置:网站首页>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
边栏推荐
- 基础贪心总结
- How do you think the fund is REITs? Is it safe to buy the fund through the bank
- Pytorch中named_parameters、named_children、named_modules函数
- 开源项目推荐:3D点云处理软件ParaView,基于Qt和VTK
- Why disable foreign key constraints
- Cookie&Session
- How did the computer reinstall the system? The display has no signal
- shell脚本中的DATE日期计算
- 一刷313-剑指 Offer 06. 从尾到头打印链表(e)
- The El tree implementation only displays a certain level of check boxes and selects radio
猜你喜欢
随机推荐
Sorting and replying to questions related to transformer
The length of the last word of the string
字符串最后一个单词的长度
编译,连接 -- 笔记
One brush 314 sword finger offer 09 Implement queue (E) with two stacks
使用 Bitnami PostgreSQL Docker 镜像快速设置流复制集群
cadence SPB17.4 - Active Class and Subclass
Go语言切片,范围,集合
Mobile finance (for personal use)
单体架构系统重新架构
Go language, array, pointer, structure
Basic concepts of website construction and management
基础贪心总结
Timing model: gated cyclic unit network (Gru)
网站建设与管理的基本概念
导入地址表分析(根据库文件名求出:导入函数数量、函数序号、函数名称)
Pgpool II 4.3 Chinese Manual - introductory tutorial
Spark 算子之partitionBy
多线程原理和常用方法以及Thread和Runnable的区别
Load Balancer









