Reputation: 5676
I have a code here but always result to "alert(2)". Help me correct my code on url matching using regex global
var curl = document.URL;
var burl = document.location;
var ctitle = document.title;
var url = /"catalogsearch\/advanced"/g;
if(curl.match(url) == true) {
alert(1);
} else {
alert(2);
}
Upvotes: 0
Views: 2461
Reputation: 1794
remove ""
document.URL.match(/questions\/5328532/);
works fine
Upvotes: 0
Reputation: 3887
Instead of using a regular expression you could try this:
if(curl.indexOf("catalogsearch/advanced") != -1) {
etc.
Upvotes: 0
Reputation: 19271
Remove the quotes from the regexp and use the regexp test method:
var curl = document.URL;
var burl = document.location;
var ctitle = document.title;
var url = /catalogsearch\/advanced/g;
if(url.test(curl)) {
alert(1);
} else {
alert(2);
}
Upvotes: 2