正则表达式匹配字符之间的所有内容

时间:2020-11-08 23:09:44

标签: regex

我有一个看起来像这样的网址:/events/eventID/something

我该如何匹配eventID 无论eventID有多长,它都应该起作用。

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:-2)

您可以使用前瞻:

x(?=y) – positive lookahead (matches 'x' when it's followed by 'y')
x(?!y) – negative lookahead (matches 'x' when it's not followed by 'y')

,您还可以使用lookbehinds:

(?<=y)x – positive lookbehinds (matches 'x' when it's precede by 'y')
(?<!y)x – negative lookbehinds (matches 'x' when it's not precede by 'y')

您的情况的示例:

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

(?<=.\/).+(?=\/)