我正在尝试将可生成的网址与任意随机数匹配,例如,这些网址:
#include <stdio.h>
//int mystrcmp(char * s, char * t);
int main(int argc, char * argv[])
{
// #1 Prints out cmd line arguments
int i;
for (i = 0; i < argc; ++i)
{
printf("%s\n",argv[i]);
}
printf("\n"); //Spaceholder
// #2 String Compare
printf("%s %s\n", argv[1], argv[2]);
printf("Returned Value: %d\n", mystrcmp(argv[1], argv[2]));
//mystrcmp(argv[1],argv[2]);
// #3 String Concatenate
printf("\n"); //Spaceholder
printf("String Concatenate: %s\n", mystrcat(argv[1]));
// #4 String Copy
printf("\n"); //Spaceholder
// printf("String Copy: %s\n" , mystrcpy(argv[1]));
}
///////////
//Methods//
///////////
/* srtcmp: return < 0 if s < t, 0 if s==t, > 0 if s > t */
int mystrcmp(char * s, char * t)
{
int i;
for(i = 0; s[i] && s[2]; ++i)
{
if (s[i] == t[i] || (s[i]) == t[i])
continue;
else
break;
}
if (s[i] == t[i])
return 0;
if ((s[i]) < (t[i]))
return -1;
return 1;
}
mystrcat(char *dest, char *source)
{
int a = 0;
while(*(dest + a) != '\0')
{
a++;
}
int b = 0;
while(*(source + a) != '\0')
{
*(dest + a) = *(source + b);
a++;
b++;
}
}
mystrcpy(char * s, char * dest)
{
while((*s++ = *dest++)!= '\0')
;
return *dest;
}
有一个代码来比较网址与随机数是相等的,这个当前代码不起作用。
www.domain.com/products/id?=292
www.domain.com/products/id?=132
www.domain.com/products/id?=698
我做了一个jsfiddle,IEEE Std 1800-2012我正在寻找一个解决方案,可以使用随机数匹配的url返回true
编辑:我出于某种原因无法接受url3.match(numberPat),我需要完全按照:
由于选择性比较,www.domain.com/products/id?= + numberPat 。
答案 0 :(得分:2)
您希望在正则表达式/\d+/
中使用重复来匹配多个数字。
另外,为什么要尝试将正则表达式添加到String?
如果您想查看网址,您可能正在寻找
url1.match(/^www\.domain\.com\/products\/id\?=\d+$/)
^
从头开始www\.domain\.com\/products\/id\?=
找到这个确切的字符串\d+
找到任意位数$
必须是字符串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'));
修改强>:
再次阅读您的问题,我认为您正在拍摄匹配网址的数字,然后比较
if (url3 == "www.domain.com/products/id?=" + url3.match(/\d+/))`
这有点多余,因为你也可以匹配字符串的静态部分。
修改最少的代码: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")
}
答案 1 :(得分:2)
正如我在评论中建议的那样,你可以在没有正则表达式的情况下做到这一点:
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");
}
注意:如果您有多个密钥,这将会中断。
示例强>
答案 2 :(得分:1)
要动态构建正则表达式,请使用new RegExp(...)
,如下所示:
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