Matt B
Matt B

Reputation: 99

Regex to capture trailing slash and query parameters but doesn't match if there's a file/directory after the trailing slash

I'm struggling to create a Regex that will capture if the string:

But doesn't match something like /foo or /foo/bar.

So far I'm here:

(|/|/?\?.+)

but it's still matching /foo and /foo/bar

Upvotes: 0

Views: 502

Answers (1)

anubhava
anubhava

Reputation: 785286

You may use this regex for your job:

^/?(\?.+)?$

RegEx Demo

RegEx Details:

  • ^: Start
  • /?: Match optional / at the start
  • (\?.+)?: Match string starting with ? till end with 1+ characters after ?`
  • $: End

Upvotes: 2

Related Questions