0

I need to parse long urls and set a variable (category) equal to one of the /folders/ in the path.

For example, a url that is

http://example.com/community/home/whatever.html

I need to set the variable equal to whatever folder path comes after /home/ in that url.

I've got this to alert me with what comes after /community/, but then the url turns to NaN and the link doesnt work. I think I'm not on the right track.

if ($(this.href*='http://example.com/community/')){

  var category = url.split("community/");

  alert(category[category.length - 1]);

}

Thoughts?

TIA.

7
  • sorry, I need to set the variable equal to whatever folder path comes after /community/ in that url. Commented Aug 1, 2011 at 3:56
  • 2
    I'm not getting that this.href*= thing at all. What is that supposed to mean? Commented Aug 1, 2011 at 4:01
  • jquery selector for if this href attribute contains example.com/community Commented Aug 1, 2011 at 4:08
  • As the OP asked, He just has the IDEA of doing thing. So we have to give his idea an implementations. Commented Aug 1, 2011 at 4:09
  • I think $(this.href*='http://example.com/community/') is suppose to be $('[href*="http://example.com/community/"]')... /me shrugs Commented Aug 1, 2011 at 4:11

2 Answers 2

2

You can fetch everything after the "/community/" with a regular expression:

var url = "http://www.example.com/community/whatever";
var category = "";
var matches = url.match(/\/community\/(.*)$/);
if (matches) {
    category = matches[1];   // "whatever"
}

Working example here: http://jsfiddle.net/jfriend00/BL4jm/

If you want to get only the next path segment after community and nothing after that segment, then you could use this:

var url = "http://www.example.com/community/whatever/more";
var category = "";
var matches = url.match(/\/community\/([^\/]+)/);
if (matches) {
    category = matches[1];    // "whatever"
} else {
    // no match for the category
}

Workikng example of this one here:http://jsfiddle.net/jfriend00/vrvbT/

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

4 Comments

that works, but what if i were to want to return whatever, not whatever/asdf: jsfiddle.net/BL4jm/2 – ben 2 mins ago edit
@ben - I updated my answer with a 2nd option to get only the "whatever" part.
this doesn't help me with only doing this IF the string in the href matches that pattern, but i've got something that will work by setting category = "Doesn't Match", and then overwriting it if it matches. thanks mate!
@ben - If you want to execute code when it doesn't match, then just put an else on the end of the if.
0

When you do this.href*= you're doing multiplication, and that's why you're getting not-a-number. It multiplies this.href by the string and assigns that to href.

If you mean to test whether the url starts with that string you can do it like this, no need for jQuery:

var start = 'http://example.com/community/';
if (url.substring(0, start.length) === start)){
  var category = url.split("community/");
  var lastPart = category[category.length - 1];
  return lastPart.split("/")[0];
}

1 Comment

@ben: just split again by "/" and return the first result. See my answer.

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.