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