Reputation: 6893
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
Reputation: 67882
Your regex will only match values beginning with connect.
You probably want this:
if(path.match(/^.*connect.*$/)) {
Upvotes: 1
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