regex - 正则表达式匹配没有斜杠的 URL,也没有文件扩展名
问题描述
到目前为止,我对阅读这么多关于此的正则表达式文章感到非常困惑。
我希望匹配第一个 URL,其余的不应匹配:
https://subdomain.example.com/test <== only this should match
https://subdomain.example.com/paht/test.css
https://subdomain.example.com/path/path/test.js
https://example.com/test/
我希望仅匹配没有尾部斜杠或文件扩展名的路由。
这是我的正则表达式:https:.*^(?!([^\/]|(\.[a-z]{2,8})))$
你可以在这里试试:https ://regexr.com/5dic8
解决方案
Use
^https?:\/\/(?:.*\/)?[^\/.]+$
See proof
Explanation
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
http 'http'
--------------------------------------------------------------------------------
s? 's' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
: ':'
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
[^\/.]+ any character except: '\/', '.' (1 or more
times (matching the most amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
推荐阅读
- reactjs - React - 突出显示表格中的选定行
- java - android sqlite 使用 db.delete 删除超过 x 天的行
- javascript - 无法读取未定义的属性“标签”
- c++ - 非类型模板参数和要求
- mysql - 查询内连接3张表
- python - 调用带有参数的.Net函数形式python(通过引用传递)
- java - 所有后续 servlet 请求都可以立即写入 ConcurrentHashMap 吗?
- sql - 将复杂的 Pivot SQL 查询转换为 Laravel Eloquent 关系
- javascript - 我对 Firebase .setBackgroundMessageHandler 的调用有什么问题?
- python-3.x - python 3 - 为什么在寻找最大 ord() 值时 for 循环不起作用?