Toni2708 Toni2708 - 1 month ago 22
Javascript Question

Easy RegExp doesnt match

I want to match those URLs

de-de/checkout/
de-de/checkout/confirm/


with a RegExp. I've tried
/(.*)\/checkout\/(|$|confirm\/$)/g
, but I only get two matches

de-de/checkout/
de-de/checkout/


It's probably very easy but I don't have a clue why this isn't working.

Thanks for your help!

Answer

This is a bit easier to follow in my opinion:

(.+?)?\/checkout\/(?:confirm\/)?

Although I'm unsure whether you need to simply match the url (true/false) or capture de-de or whatever precedes or follows the samples you provide.

confirm is wrapped into a non-capturing group because it's the only path component which may or may not be there.