当前位置:网站首页>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
边栏推荐
- One brush 313 sword finger offer 06 Print linked list from end to end (E)
- Neodynamic Barcode Professional for WPF V11.0
- Demonstration meeting on startup and implementation scheme of swarm intelligence autonomous operation smart farm project
- [self entertainment] construction notes week 2
- Temporal model: long-term and short-term memory network (LSTM)
- Accumulation of applet knowledge points
- C language --- string + memory function
- 导入地址表分析(根据库文件名求出:导入函数数量、函数序号、函数名称)
- 王启亨谈Web3.0与价值互联网“通证交换”
- Deletes the least frequently occurring character in the string
猜你喜欢
网站压测工具Apache-ab,webbench,Apache-Jemeter
Use bitnami PostgreSQL docker image to quickly set up stream replication clusters
大型互联网为什么禁止ip直连
Neodynamic Barcode Professional for WPF V11.0
Spark 算子之partitionBy
WPS品牌再升级专注国内,另两款国产软件低调出国门,却遭禁令
Mumu, go all the way
Temporal model: long-term and short-term memory network (LSTM)
Demonstration meeting on startup and implementation scheme of swarm intelligence autonomous operation smart farm project
Cookie&Session
随机推荐
Cookie&Session
fatal error: torch/extension. h: No such file or directory
Configuration of multi spanning tree MSTP
PHP 的运算符
s16. One click installation of containerd script based on image warehouse
Go language, condition, loop, function
编译,连接 -- 笔记
腾讯Offer已拿,这99道算法高频面试题别漏了,80%都败在算法上
utils.DeprecatedIn35 因升级可能取消,该如何办
C language --- advanced pointer
Upgrade MySQL 5.1 to 5.611
JVM - Chapter 2 - class loader subsystem
Why disable foreign key constraints
cadence SPB17.4 - Active Class and Subclass
Pytorch中named_parameters、named_children、named_modules函数
王启亨谈Web3.0与价值互联网“通证交换”
小程序知识点积累
Fastjon2他来了,性能显著提升,还能再战十年
Introduction to dynamic programming of leetcode learning plan day3 (198213740)
北京某信护网蓝队面试题目