Reputation: 1241
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.
Upvotes: 0
Views: 5094
Reputation: 10675
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+$/)
^
Start at the beginningwww\.domain\.com\/products\/id\?=
find this exact string\d+
find any number of digits$
must be end of stringhttp://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")
}
Upvotes: 2
Reputation: 784928
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
Upvotes: 2
Reputation: 14530
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
Upvotes: 2