0

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);
}
1
  • Can you try: var url = /catalogsearch\/advanced/g; Commented Mar 16, 2011 at 16:43

3 Answers 3

2

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);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of using a regular expression you could try this:

if(curl.indexOf("catalogsearch/advanced") != -1) {
etc.

Comments

0

remove ""

document.URL.match(/questions\/5328532/);

works fine

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.