I'm trying to match a URL which it can be generated with any random numbers, for example, these urls:
www.domain/products/id?=292
www.domain/products/id?=132
www.domain/products/id?=698
There a code to pare URL with random number is equal, this currently code won't work.
var url1= "www.domain/products/id?=292";
var url2= "www.domain/products/id?=7542";
var url3= "www.domain/products/id?=5401";
var numberPat = /[0-9]/;
if(url3 == "www.domain/products/id?=" + numberPat){
alert("Domain with random number is MATCHED")
}
else{
alert("Domain with random number is NOT matched")
}
I made a jsfiddle, / 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/products/id?=+numberPat due to selective parison.
I'm trying to match a URL which it can be generated with any random numbers, for example, these urls:
www.domain./products/id?=292
www.domain./products/id?=132
www.domain./products/id?=698
There a code to pare URL with random number is equal, this currently code won't work.
var url1= "www.domain./products/id?=292";
var url2= "www.domain./products/id?=7542";
var url3= "www.domain./products/id?=5401";
var numberPat = /[0-9]/;
if(url3 == "www.domain./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/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./products/id?=+numberPat due to selective parison.
Share edited Oct 9, 2015 at 21:31 Ivan asked Oct 9, 2015 at 21:10 IvanIvan 1,2412 gold badges21 silver badges43 bronze badges 1- I would extract the value (by getting text after =) then do a isNaN to see if they are numbers. No need for regex. – Script47 Commented Oct 9, 2015 at 21:12
3 Answers
Reset to default 2You 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\.\/products\/id\?=\d+$/)
^
Start at the beginningwww\.domain\.\/products\/id\?=
find this exact string\d+
find any number of digits$
must be end of string
http://jsfiddle/n5s2pvkr/3/
var url1= "www.domain./products/id?=292";
var url2= "www.domain./products/id?=7542";
var url3= "www.domain./products/id?=5401";
function isMatch(url) {
if(url.match(/^www\.domain\.\/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 paring it like
if (url3 == "www.domain./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/n5s2pvkr/4/
var url1= "www.domain./products/id?=292";
var url2= "www.domain./products/id?=7542";
var url3= "www.domain./products/id?=5401";
var numberPat = /[0-9]+/;
if(url3 == "www.domain./products/id?=" + url3.match(numberPat)){
alert("Domain with random number is MATCHED")
}
else{
alert("Domain with random number is NOT matched")
}
As I suggested in my ment you can do this without regex:
var url2= "www.domain./products/id?=7542";
var url3= "www.domain./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/dxyt8Ldm/
To build your regex dynamically use new RegExp(...)
like this:
var url1= "www.domain./products/id?=292";
var url2= "www.domain./products/id?=7542";
var url3= "www.domain./products/id?=5401";
numberPat = '[0-9]+';
// build your regex here
var re = new RegExp("www\\.domain\\./products/id\\?=" + numberPat)
// now test it
re.test(url3)
true
re.test(url2)
true
re.test(url1)
true