当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
Why disable foreign key constraints
APISIX jwt-auth 插件存在错误响应中泄露信息的风险公告(CVE-2022-29266)
KNN, kmeans and GMM
Interview questions of a blue team of Beijing Information Protection Network
The length of the last word of the string
一刷314-剑指 Offer 09. 用两个栈实现队列(e)
Basic greedy summary
Upgrade MySQL 5.1 to 5.611
C language --- advanced pointer
Use bitnami PostgreSQL docker image to quickly set up stream replication clusters
贫困的无网地区怎么有钱建设网络?
cadence SPB17. 4 - Active Class and Subclass
MySQL集群模式與應用場景
Go language, array, pointer, structure
Application case of GPS Beidou high precision satellite time synchronization system
单体架构系统重新架构
utils.DeprecatedIn35 因升级可能取消,该如何办
北京某信护网蓝队面试题目
Go并发和通道
pgpool-II 4.3 中文手册 - 入门教程









