2

I'm struggling with what is probably a very simple regex problem. I'm working on a simple prototype and need to know what page I'm on, so I don't reload it if the user clicks a menu widget to navigate to the same view.

I have two URLs the user can switch between:

http://localhost/TestMVC/Larry/LarryTiles

http://localhost/TestMVC/Larry/LarryTilesList

The URLs can also have some trailing querystring items, like this: http://localhost/TestMVC/Larry/LarryTilesList?filterValue=servers

LarryTiles is giving me the problem. "/\bLarryTiles\b/" worked on Friday (after answers from other questions here) but this doesn't match now. :)

I need to find exactly the strings "LarryTiles" and "LarryTilesList" in these two URLs but can't quite figure out how to do that. The URL changes between my local machine and the various servers where it's hosted, so I can't rely on position.

EDIT: added the example with a trailing querystring, which I'd forgotten. Sorry :(

3 Answers 3

2

You can get the last path segment of an URL like this:

function getLastPathSegment(url) {
    var match = url.match(/\/([^\/]+)\/?$/);
    if (match) {
        return(match[1]);
    }
    return("");
}

// returns "LarryTiles"
getLastPathSegment("http://localhost/TestMVC/Larry/LarryTiles");     

// returns "LarryTilesList"
getLastPathSegment("http://localhost/TestMVC/Larry/LarryTilesList");

So, you could do this:

var endPath = getLastPathSegment(window.location.pathname);
if (endPath == "LarryTiles") {
    // some code
} else if (endPath == "LarryTilesList") {
    // some code
} else {
    // some code
}
Sign up to request clarification or add additional context in comments.

3 Comments

Per new info from OP, I updated my example to use window.location.pathname so any query string or hash tag is already stripped off before comparison.
I'm testing this, but VS and Chrome both say there is an error in your regex.
@dex3703 - Yes, sorry there was a typo that I've fixed.
0

You can use this code:

str = 'http://localhost/TestMVC/Larry/LarryTiles?filterValue=servers';

if (str.match(/\/([^\/?]+)(?=\/$|\?|$)/)) {
   if (match[1] == 'LarryTiles')
       alert('LarryTiles found');
   else if (match[1] == 'LarryTilesList')
       alert('LarryTilesList found');
}

4 Comments

This is vulnerable to a trailing slash.
Agreed but URLs in OP's question don't have trailing slash.
Based on your latest edited question I updated my answer to handle trailing slash, query string etc cases.
Thanks. I note this will find both "LarryTiles" and "/LarryTiles", but I just take match[1] as you have above. Works great. That's one scary regex!
0

Seems like what you explained works, otherwise try this: http://jsfiddle.net/Wfz9d/

Do you have a case-sensitivity issue?

1 Comment

Case isn't an issue, though these are the URLs being used.

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.