0

I'm trying to make navigation on click.

Structure of the site is simple:

site.com/page_2/ site.com/page_3/ ... site.com/page_n/ 

How to get the URL of the page number?

6 Answers 6

3

Something like this should work:

> 'site.com/page_123/'.match(/page_(\d+)/)[1]; "123" 
Sign up to request clarification or add additional context in comments.

Comments

1
"site.com/page_123/".match(/(?:page_)(\d+)/)[1]; 

1 Comment

?: is not really needed. /page_(\d+)/ is enough.
0

Or why complicate things with regex when you can substring as well:

"site.com/page_2/".substring("mysite.com/page_".length, name.lastIndexOf("/")); 

Comments

0

One more non regex solution:

parseInt("site.com/page_123/".split("page_").pop(), 10); 

Comments

0
var s = window.location.href; var x = s.split("/"); for(var i = 0; i < x.length; i++) { if(i==x.length-2) { var c = x[i].match(/page_(\d+)/)[1]; } } 

Automatically gets the Page Number ;) ​ Working Fiddle

2 Comments

Why use a loop when you're going to act only once in a known index? Why not just x[ x.length - 2 ].match(/page_(\d+)/)[1];?
oh yeah thats the point but still gives what he want! @Juhana thanks :)
0

find the fiddle here : http://jsfiddle.net/N2WRS/

var url = "http://www.site.com/page_2/"; // window.location var nurl = url.substr(0, url.lastIndexOf('/')); var finalUrl = nurl.substr(nurl.lastIndexOf('/')+1); alert(finalUrl); // <---this will get you the page_2 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.