views:

221

answers:

2

Basically, if I'm at: http://example.com/content/connect/152, I want to find out if "connect" is present in the url and then set the selected value of a menu to something specific... (The url could also be something like http://example.com/content/connections, in which case, it should still match...)

This is what I've been trying, which, clearly isn't working....

var path = window.location.pathname;
if(path).match(/^connect) {
 $("#myselect").val('9');
} else {
 $("#myselect").val('0');
}
A: 

Your regex will only match values beginning with connect.

You probably want this:

if(path.match(/^.*connect.*$/)) {
Stefan Kendall
Again, the same typo as I've described in OP's comment. Should've been if(path.match(/^.*connect.*$/)) { ...
Ondrej Slinták
if(path.match(/^.*connect.*$/)) {
ryanulit
+1  A: 

Since connect can be anywhere in your URL there is no need to add the ^

try :

if (path.match("/connect"))

this assume that you want a "/" right before a connect

Patrick
Thank you... (and everyone else!)
n00b0101