Search code examples
regexstringurlregular-language

How to regex last part of URL only?


I want to do a regex that target the end of a url:

www.company.com/orders/thanks

If you return from email or account page order ID is populated in the end:

www.company.com/orders/thanks/1sfasd523425

So I only want to target the URL that ends with /thanks

This thread bring is similiar: How do I get the last segment of URL using regular expressions

Had something similair .*\/thanks\/.+ but target incorrectly.

EDIT: Only target URLs ending with /thanks or /thanks/


Solution

  • Try with lookahead like this.

    Regex: .+(?=\/thanks$).+

    Explanation: This will match the URL only if thanks is at end of string by positive lookahead.

    Regex101 Demo