dewastator
dewastator

Reputation: 213

Removing url params using regex

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?

Upvotes: 1

Views: 62

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626806

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.

Upvotes: 1

Related Questions