user3276426
user3276426

Reputation: 23

How can i find part of the string in url address with javascript?

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 ?

Upvotes: 0

Views: 40

Answers (2)

mrrodd
mrrodd

Reputation: 126

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];

Upvotes: 0

Timeout
Timeout

Reputation: 7909

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"

Upvotes: 1

Related Questions