regex - JS 正则表达式匹配 URL 的所有部分,忽略由该 URL 中的动态值组成的特定路径
问题描述
正则表达式应匹配的示例 URL:
https://domain/a/b/c/d/e/
i39m33rgp5jcrohl5atwe4c9/g/h.file
这不应该匹配:i39m33rgp5jcrohl5atwe4c9
我在想一些类似的事情,但https://regex101.com/上的测试似乎与它不匹配。任何指针?
^/a/([^/]+/)?([^/]+/)?([^/]+/)?([^/]+/)?([^/]+/)?([^/]+/)(\.file)?$
解决方案
使用您显示的示例,您能否尝试以下操作。
(^https?:\/\/(?:.*?\/){6}).*?\/(.*)$
说明:为上述添加详细说明。
( ##Starting 1st capturing group here.
^https?:\/\/ ##Checking condition if value starts from http/https://
(?:.*?\/){6} ##In a non-capturing group doing non-greedy match till / up to 6 times, just before dynamic value in URL.
) ##Closing 1st capturing group here.
.*?\/ ##Again going non-greedy match till next occurrence of / here.
(.*)$ ##In 2nd capturing group having everything, rest of URL here.
推荐阅读
- powershell - 运行没有错误且没有结果的 Powershell 脚本
- c++ - 如果使用 goto 将控制权转移到 if(false) 块会发生什么?
- javascript - 为什么执行一个函数后进程没有任何错误就结束了?
- java - 有没有办法在java中获取实例名称?
- dialogflow-es - 在 Dialogflow 中添加条件
- mysql - Python子进程运行mysql转储恢复
- java - 如何使用 maven 从 protobuf 为 Java 目标 11 生成 java 存根?
- quantum-computing - qiskit VQC 带有用于状态准备的幅度编码
- c++ - 在我的程序中更改共享库中定义的变量不会从共享库中反映出来
- c++ - WSL2 上的设备内存分配失败