0

I have a problem with removing everything after the last slash of URL in Elixir. For instance, I have URL:

http://localhost:4000/admins/new

I wanna change it to:

http://localhost:4000/admins

How can I do it?

2 Answers 2

1

The safest way would probably be to use URI.parse/1

uri = URI.parse("http://localhost:4000/admins/new")

%URI{uri |
  path:
    uri.path
    |> String.split("/")
    |> Enum.slice(0..-2)
    |> Enum.join("/")
}
|> URI.to_string()

#⇒ "http://localhost:4000/admins"
Sign up to request clarification or add additional context in comments.

Comments

0

You can do it with a regular expression:

Regex.replace(~r{/[^/]+$}, "http://localhost:4000/admins/new", "")

Output:

"http://localhost:4000/admins"

Comments

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.