0

I thought I'd rewrite this and add a bit of context (more so it'll be easier to explain to me) .

Basicaly I'm capturing the back/forward buttons with the popstate event like so :

 window.addEventListener("popstate", function(e) { 


     curstate = window.location.href ;

Then using a switch statement to determine what to do based on the new state :

     switch (curstate) {

        case 'http://mydomain.com/':

            navi('about', 200);
            break;



        case 'http://mydomain.com/resume':
            navi('resume', 200);
            break;
        };

    });

The problem comes when the state is mydomain.com/portfolio/-whatever-

I need a case : starts with mydomain.com/portfolio/(capture this bit of the uri) : then put the captured bit in a variable eg currentpage = capturedbitofuri .

2
  • /^mydomain\.com\/portfolio\/(.*)$/ Captured after it. What do you mean by pop? Commented Feb 12, 2012 at 21:15
  • Not as in array_pop, he just means "put it in a variable" Commented Feb 12, 2012 at 21:17

3 Answers 3

1
// returns an array on success or null on failure
var match = /^mydomain\.com\/portfolio\/(.*)/.exec(str); 
if (match) {
   // match[1] contains the group (.*), which is everything after the prefix
   return match[1]; 
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Dor , I've got the following set up like so curstate = window.location.href ; var match = /^mydomain\.com\/portfolio\/(.*)/.exec(curstate); console.log(match); if (match) { // match[1] contains the group (.*), which is everything after the prefix console.log(match[1]); }; but all I get is a null from match.
0

Fairly straightforward: /^mydomain\.com\/portfolio\/(.*)/

The / characters must be escaped, as we're using / as the delimiter for the pattern.

^ at the start of the pattern indicates that the string must start with "mydomain...".

At the end, the .* means "anything" and the ( ) around it means "capture this in a match".

As for using it, have a read over regex in JS 101, it'll give you a good introduction to regex :)

7 Comments

And capturing what's after it?
@Joe In JavaScript, a RegExp is surrounded by slashes (/). Literal /s have to be escaped: \/.
True :) Good catch, dropped into PHP mode there haha
ok (sorry this is a bit new to me) , i need it in too parts , one part to check it it starts with that and I use the variable within the if statement .
javascriptkit.com/javatutors/re.shtml - give that a quick read @FrankAstin, it's a good basic 101 for regex in JS
|
0

You don't even need regex. All you need to wrap your head around is .indexOf and .substring.

if (curstate === 'http://mydomain.com/') {

    navi('about', 200);

} else if (curstate === 'http://mydomain.com/resume')

    navi('resume', 200);

} else if (curstate.indexOf('http://mydomain.com/portfolio') > -1) {
    var base = 'http://mydomain.com/portfolio';
    portfoliosection = curstate.substr(base.length + 1, curstate.length);
}

2 Comments

I got there slightly differently , but this looks like a neater option . Thanks Devin .
yeah I like that you don't have to rack your brain (or anyone else working with your code) on figuring out regular expressions.

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.