0

I'm struggling with a regex that I wish to match all 'valid' variants for a path.

My definition of valid is that it can be followed by an optional slash, and optional query string or single '?

It needs to work in both PHP and JavaScript so positive/negative look behind is out of the question.

There's a starting point to tinker with here http://regexr.com/3drov

At time of writing I've got ^\/page-title\/?(?=\?).*$ where I thought the \/? would allow me to match the first two lines, as an optional slash. But I think I've just got confused now.

I tried putting in a conditional (?(?=)then|else) type thing, which I thought was supported in JS but was getting a syntax error from regexr.com

So, any help would be really appreciated. Thanks!

These should match for the input: /page-title

/page-title
/page-title/
/page-title?
/page-title/?
/page-title?foo
/page-title/?foo=bar

These should not

/page-title/foo/
/page-title-foo-bat

Update

I hadn't realised that the classic conditional syntax was not supported by JavaScript. After reading this post (I would like to mimick conditionals in javascript regex) I seem to have it working with ^\/page-title\/?((?=\?).*|)$

Saved the update to test here: http://regexr.com/3drq0

Community
  • 1
  • 1
joevallender
  • 4,293
  • 3
  • 27
  • 35

3 Answers3

1

I used a negative lookahead inside my positive lookahead to negate any option with an additional \, check if this works correctly for you: ^(?=\/page-title\/?\??(?!(.+\/)|-)).+

DEMO

Anshul Rai
  • 772
  • 7
  • 21
1

This should do it:

^\/page-title[\/\?]?(\?[^\/]*)?$

Or

^\/page-title(\/|\?)?(\?[^\/]*)?$

offchance
  • 642
  • 7
  • 13
  • Thanks Faisal, but when I test it, it matches everything – joevallender Jul 21 '16 at 07:15
  • @joevallender I've missed the last bit of your question. I've updated the regex in my answer above. – offchance Jul 21 '16 at 07:27
  • Thanks, I'll test it now. If you see above, I seem to have got it working with the first half of an if/else equivalent – joevallender Jul 21 '16 at 07:36
  • Out of the two, in your opinion which one is more intuitive? In words mine is "Optional slash, then any number of characters afterwards if the next is a question mark". – joevallender Jul 21 '16 at 07:38
  • @joevallender I've tested both with some of my own test-cases. They both seem to be doing a good job. At this stage, I'd choose either the one that's more readable, or the one that passes more tests. For instance check the how they behave for this url `/page-title/?test=A0C3C1D163BE880A?hl=en_US&fs=1?t/stes%2s` (not the same way) – offchance Jul 21 '16 at 07:53
  • Interesting point, thanks. They are both working reliably in both PHP and JS for my limited test scenarios. I'll have a think about what constraints of my data set and environment apply :) – joevallender Jul 21 '16 at 07:55
1

perhaps this:

^\/page-title\/?(\?.*)?$

start /page-title

optional /

optional (? followed by anything or nothing )

end

http://regexr.com/3drpb

Jasen
  • 11,837
  • 2
  • 30
  • 48