1

I have problem with removing params from url starting on ":". I have example path like:

/foo/:some_id/bar/:id

I would like to archive following result:

/foo/bar

I tried to create some regex. I figured out this:

\/:.*?\/

But this one removes :some_id but still It leaves :id part. Can you tell me how I can modify my regex to remove all params?

2
  • 1
    split by /. iterate through items. if it doesnt start with :, append it. Commented May 9, 2016 at 9:17
  • This should help you: regex101.com/r/tG0oG0/1 Commented May 9, 2016 at 9:20

1 Answer 1

1

Your regex requires a / to be present at the end. You cannot just remove the / from the regex since .*? won't match anything then. Use a negated character class:

\/:[^\/]+

See the regex demo

Pattern details:

  • \/: - matches a literal /:
  • [^\/]+ - matches 1+ characters other than / as [^...] defines a negated character class matching all characters but those defined in the class.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for solution and explanation ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.