2

Given the url (string) http://stackoverflow.com/questions/ask, how could I split it up into an array that contains data like the following: questions,ask. How would I make this work for an infinite amount of slashes?

The way I am thinking of doing it would be to use URL.parse in Node.JS and then use STRING.split('/') to separate the string into an array.

Would this be the correct way to do it? Is there a quicker way?

1
  • 1
    URL.parse does the dirty work for you (getting the path), leaving you the easy part. I'd say splitting by / is definitely a fine way to go to get each "sub-page into an array". You could of course use a regular expression, but I feel like it's more straightforward/readable with the split Commented Mar 12, 2014 at 22:13

1 Answer 1

2

To the best of my knowledge, that is the best way to do it. Alternatively you could just ignore the first parts of the split:

URL_STRING.split('/').slice(3)
Sign up to request clarification or add additional context in comments.

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.