n00b0101
n00b0101

Reputation: 6893

jquery - trying to use pattern matching with window.location.pathname

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');
}

Upvotes: 1

Views: 8585

Answers (2)

Stefan Kendall
Stefan Kendall

Reputation: 67882

Your regex will only match values beginning with connect.

You probably want this:

if(path.match(/^.*connect.*$/)) {

Upvotes: 1

Patrick
Patrick

Reputation: 15717

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

Upvotes: 5

Related Questions