0

I'm trying to match a URL which it can be generated with any random numbers, for example, these urls:

www.domain.com/products/id?=292
www.domain.com/products/id?=132
www.domain.com/products/id?=698

There a code to compare URL with random number is equal, this currently code won't work.

var url1= "www.domain.com/products/id?=292";
var url2= "www.domain.com/products/id?=7542";
var url3= "www.domain.com/products/id?=5401";


var numberPat = /[0-9]/;    

    if(url3 ==  "www.domain.com/products/id?=" + numberPat){ 
        alert("Domain with random number is MATCHED")
    }
    else{
        alert("Domain with random number is NOT matched")
    }

I made a jsfiddle, http://jsfiddle.net/n5s2pvkr/1/ Im looking a solution that could return as true with matched url with random number

EDIT: I cannot accept url3.match(numberPat) for some reason, I need to do exactly like:

www.domain.com/products/id?=+numberPat due to selective comparison.

1
  • I would extract the value (by getting text after =) then do a isNaN to see if they are numbers. No need for regex. Commented Oct 9, 2015 at 21:12

3 Answers 3

2

You want to use repetition in your regex /\d+/ to match more than one digit.

Also, why are you trying to add a regex to a String?

If you want to check the URL, you're probably looking for

url1.match(/^www\.domain\.com\/products\/id\?=\d+$/)

  1. ^ Start at the beginning
  2. www\.domain\.com\/products\/id\?= find this exact string
  3. \d+ find any number of digits
  4. $ must be end of string

http://jsfiddle.net/n5s2pvkr/3/

var url1= "www.domain.com/products/id?=292";
var url2= "www.domain.com/products/id?=7542";
var url3= "www.domain.com/products/id?=5401";

function isMatch(url) {
    if(url.match(/^www\.domain\.com\/products\/id\?=\d+$/)){ 
        return "Domain with random number is MATCHED";
    }
    else{
        return "Domain with random number is NOT matched";
    }
}

alert([url1, url2, url3].map(function(url) {
    return isMatch(url);
}).join('\n'));

Edit:

Reading your question again, I think you were shooting matching the digits of the URL then comparing it like

if (url3 == "www.domain.com/products/id?=" + url3.match(/\d+/))`

which is a little redundant since you could just match on the static part of the string as well.

Code with fewest modifications: http://jsfiddle.net/n5s2pvkr/4/

var url1= "www.domain.com/products/id?=292";
var url2= "www.domain.com/products/id?=7542";
var url3= "www.domain.com/products/id?=5401";


var numberPat = /[0-9]+/;    

if(url3 ==  "www.domain.com/products/id?=" + url3.match(numberPat)){ 
    alert("Domain with random number is MATCHED")
}
else{
    alert("Domain with random number is NOT matched")
}
Sign up to request clarification or add additional context in comments.

1 Comment

It actually worked what I excepted, thank you for reading my reason!
2

As I suggested in my comment you can do this without regex:

var url2= "www.domain.com/products/id?=7542";
var url3= "www.domain.com/products/id?=5401";

if (isNaN(url2.split("=")[1]) == false) {
    alert("Is number.");   
} else {
    alert("NAN");   
}

Note: this will break if you have multiple keys.

Example

http://jsfiddle.net/dxyt8Ldm/

1 Comment

It didn't help me because it only needs to match ...products/id?=234, not ...profile/id?=234, ...store/id?=234, ...promotion/id?=234
2

To build your regex dynamically use new RegExp(...) like this:

var url1= "www.domain.com/products/id?=292";
var url2= "www.domain.com/products/id?=7542";
var url3= "www.domain.com/products/id?=5401";
numberPat = '[0-9]+';

// build your regex here
var re = new RegExp("www\\.domain\\.com/products/id\\?=" + numberPat)

// now test it
re.test(url3)
true
re.test(url2)
true
re.test(url1)
true

1 Comment

Not sure how you tried. If you copy/paste above code in JsFiddle it will work fine.

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.