首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >正则表达式,用于获取包含URL的短横杠和正斜杠的文件名

问正则表达式,用于获取包含URL的短横杠和正斜杠的文件名
EN

Stack Overflow用户
提问于 2021-03-07 18:38:12
回答 3查看 35关注 0票数 0

我正在尝试为这样的链接创建正则表达式

<link rel="canonical" href="https://example.com/locs/my-locs" />

我想让我的-locs作为正则表达式

代码语言:javascript
复制
(?:[^/][\d\w\.]+)$(?:")

这是我一直在尝试的

EN

回答 3

Stack Overflow用户

发布于 2021-03-07 18:45:34

尝试以下模式:

(?<=\/)[^\/]+(?=\")

请参阅Demo

更新

(href=\".*?)(?<=\/)([^\/<> ]+)(?=\")

Demo

票数 1
EN

Stack Overflow用户

发布于 2021-03-07 18:42:00

下面的代码还确保它以http或https开头,并对urls进行了其他几个检查:https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)

票数 0
EN

Stack Overflow用户

发布于 2021-03-08 05:36:36

使用

代码语言:javascript
复制
(?<=\/)[\w-]+(?="\s*\/>)

参见proof。

说明

代码语言:javascript
复制
--------------------------------------------------------------------------------
  (?<=                     look behind to see if there is:
--------------------------------------------------------------------------------
    \/                       '/'
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  [\w-]+                   any character of: word characters (a-z, A-
                           Z, 0-9, _), '-' (1 or more times (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    "                        '"'
--------------------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    \/                       '/'
--------------------------------------------------------------------------------
    >                        '>'
--------------------------------------------------------------------------------
  )                        end of look-ahead
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66515560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档