Reputation: 20922
I'm currently using the following line to match this URL - www.sitename.com/join
if(/join/.test(self.location.href)) {joinPopUp();}
The catch is it will match /blahjoinblah as well.
How can I change this so it will only match the exact word 'join'.
Note: I think it will have to match '/join' as the '/' is still present (I think).
thankyou
Upvotes: 1
Views: 368
Reputation: 1081
To learn more about Regular Expression read some of these great resources.
But to answer your question more concretely, if you truly want it to match only /join
with no query string variables or other input then something like this works in which it checks for the /
and that it ends with /join
. Though I suppose that would still match /foo/join
function testUrl(url) {
if(/\/join\/?$/.test(url)) {
console.log(url, "matched /join");
}
}
testUrl(self.location.href);
testUrl("https://test.com/join");
testUrl("https://test.com/foo/join");
testUrl("http://test.com/blahjoinblah");
So you might want to switch up what you are matching - possibly look at self.location.pathname
Upvotes: 1
Reputation: 91806
The regular expression, \/join(?=\/)
may be what you are looking for. It will check for the /join
value and then uses positive look-a-head to see whether the character /
follows.
A quick test within the console:
["/xxjoin", "/join/", "/joinme", "joinme"].
forEach(function (value) {
console.log(value.match(/\/join(?=\/)/));
});
// returns the following:
null
["/join", index: 0, input: "/join/"]
null
null
or for readability,
["/xxjoin", "/join/", "/joinme", "joinme"].
filter(function(v) {
return v.match(/\/join(?=\/)/) != null;
});
// returns ['/join/']
Upvotes: 1