3

I have an application that shows photos and albums to a user. Based on current state of the application I show appropriate view. Everytime view changes I change the url, controller then gets the url value using window.location.hash

It returns the string of this form:

"photos/byalbum/albumid"
"photos/allphotos"
"photos/eachphoto/albumid/photoid"

My question is how do I parse this using javscript regular expressions to determine which view I should be showing and also to get the parameters (albumId/photoId)

2
  • Why not path.split('/') and then path[0] ~ whatnot, and so on? Commented Mar 7, 2013 at 21:19
  • 1
    Have you seen the information on Backbone's Router? This is decent tutorial as well. You're describing essentially a router pattern, so I'd take a look at how Backbone, AngularJS, Knockout, etc., handle it. Commented Mar 7, 2013 at 22:01

3 Answers 3

6

I think you are better off doing this, then regex:

"photos/eachphoto/albumid/photoid".split("/")

Then you get array that you can examine.

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

2 Comments

Doesn't really work when the path "photos/each\/ photo" is passed in.
`\` is escape character. If you have valid backslash, it's represented as "'\\", so "photos/each\\/ photo". Also, this does not look like a valid path.
1

Rather than using regex, you should probably simply split the string on "/" and examine each piece of the value for the data that you need.

var urlString = <your returned value here>;
var urlPieces = urlString.split("/");

var view = urlPieces[1];
var album = (urlPieces[2]) ? urlPieces[2] : "";
var photo = (urlPieces[3]) ? urlPieces[3] : "";

Then play with your data as you wish. :)

3 Comments

With the example paths, photos/byalbum/albumid, you would start with a 0 index, not 1. That does, however, bring up a good point about whether or not to trim leading and trailing /. To wit: jsfiddle.net/userdude/f36S7
I actually left out the "0" on purpose because, since "photos" is standard across the board, it is really irrelevant to check it. The 2nd value (index "1") seems to indicate the "view" and the first piece of data that he seemed interested in.
True enough. I think it's a good idea to start at the root of the path, but that's opinion I suppose.
1

If someone is still interesting in this question I used this lib: path-to-regexp to solve that problem.

basically you can convert an url into variables using a pattern. For example:

const pattern = 'photos/eachphoto/:albumid/:photoid'
const path = 'photos/eachphoto/10/23'
const converter = pathToRegex.match(pattern, { decode: decodeURIComponent }) 
const params = converter(path)?.params || {}

and params will be similar to: { albumid: "10", photoid: "23" }

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.