0

Good evening, I have a problem to find string from url address when I click on next page, for example I have url: http://www.example.cz/example/example-example/page/2/ <- hyphen is optinal. I need to find part http://www.example.cz/example/example-example/ <- without part page/number/ and save it to variable. Can you guys help me how to do it ?

1

2 Answers 2

1

You could split on / and then reform the string.

For example (this assumes http:// is part of the string):

"http://www.example.cz/example/example-example/page/2/".split("/").slice(0,5).join("/")
  1. Split on /
  2. Slice out the first 5 items in the array.
  3. Re-join them together with a /

Results: "http://www.example.cz/example/example-example"

Sign up to request clarification or add additional context in comments.

1 Comment

@user3276426 Yes, that property includes protocol as well.
0

If page is always in the url and also what needs to be removed, you can simply split on 'page' and take the first element of the resulting array.

var url = "http://www.example.cz/example/example-example/page/2/",
    urlParsed = url.split("page")[0];

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.