admin管理员组

文章数量:1291047

Here is an example of the url i'm trying to match: .aspx

What im trying to match is http: //store.mywebsite/folder-1 except that "folder-1" will always be a different value. I can't figure out how to write an if statement for this:

Example (pseudo-code)

if(url contains )
do this

else if (url contains )
do something else

etc

Here is an example of the url i'm trying to match: http://store.mywebsite./folder-1/folder-2/item3423434.aspx

What im trying to match is http: //store.mywebsite./folder-1 except that "folder-1" will always be a different value. I can't figure out how to write an if statement for this:

Example (pseudo-code)

if(url contains http://store.mywebsite./folder-1)
do this

else if (url contains http://store.mywebsite./folder-2)
do something else

etc

Share Improve this question edited Jun 7, 2010 at 9:29 Andy E 345k86 gold badges481 silver badges451 bronze badges asked Jun 7, 2010 at 9:27 The Muffin ManThe Muffin Man 20k30 gold badges126 silver badges196 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

In the interest of keeping things very simple...

if(location.pathname.indexOf("folder-1") != -1)
{
    //do things for "folder-1"
}

this might give you false positives if the value "folder-1" could be present in other parts of the string. If you are already making sure this is not the case, the provided example should be sufficient.

I would split() the string and check an individual ponent of the url:

var str = "http://store.mywebsite./folder-1/folder-2/item3423434.aspx"

// split the string into an array of parts
var spl = str.split("/");

// spl is now [ http:,,store.mywebsite.,folder-1,folder-2,item3423434.aspx ]
if (spl[4] == "folder-1") {
    // do something
} else if (spl[4] == "folder-2") {
    // do something else
}

Using this method it's easy to check other parts of the URL too, without having to use a regular expression with sub-expression captures. e.g. matching the second directory in the path would be if spl[5] == "folder-x".

Of course, you could also use indexOf(), which will return the position of a substring match within a string, but this method is not quite as dynamic and it's not very efficient/easy to read if there are going to be a lot of else conditions:

var str = "http://store.mywebsite./folder-1/folder-2/item3423434.aspx"
if (str.indexOf("http://store.mywebsite./folder-1") === 0) {
    // do something
} else if (str.indexOf("http://store.mywebsite./folder-2") === 0) {
    // do something
}

Assuming the base URL is fixed and the folder numbers can be very large then this code should work:

var url = 'http://store.mywebsite./folder-1/folder-2/item3423434.aspx'
  , regex = /^http:..store.mywebsite..(folder-\d+)/
  , match = url.match(regex);
if (match) {
  if (match[1] == 'folder-1') {
    // Do this
  } else if (match[1] == 'folder-2') {
    // Do something else
  }
}

Just use URL parting in JS and then you can match URL's against simple string conditions or against regular expressions

本文标签: Javascript match part of urlif statement based on resultStack Overflow