How do I get the last segment of URL using regular expressions

后端 未结 4 1244
暖寄归人
暖寄归人 2020-12-31 07:06

I have a URL:

www.domain.com/first/second/last/

How do I get the last term between slashes? i.e. last using regular expressions?

4条回答
  •  礼貌的吻别
    2020-12-31 07:46

    Here's a simple regex:

    [^/]+(?=/$|$)
    

    Should match anything you throw at it.


    If you want to look in a particular directory, use this:

    /directory.*/([^/]+)/?$
    

    and your result will be in the first capture group.

提交回复
热议问题